-1

I want to save result of script to file. Example of script:

import os

def scan_ip():
    ip2 = raw_input('Enter ip of target: ')
    target = 'nmap -sP {}'.format(ip2)
    os.system(target + > '123.txt')
martineau
  • 119,623
  • 25
  • 170
  • 301
mix world
  • 3
  • 4
  • Hello and welcome! Please review the tour https://stackoverflow.com/tour as well as how to ask a good question https://stackoverflow.com/help/how-to-ask –  Mar 19 '19 at 20:45
  • Are you trying to save the target to the txt file? – Jack Moody Mar 19 '19 at 20:56

2 Answers2

1
with open('123.txt', 'w') as my_file:
    my_file.write(target)
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
1

You should not do in this way. First you need to call the external command with output captured, and then write the output you captured to a file:

import subprocess

ip2 = raw_input('Enter ip of target: ')
p = subprocess.Popen(["nmap", "-sP", ip2], stdout=subprocess.PIPE)
out, err = p.communicate()
with open('123.txt', 'w') as f:
    f.write(out)
martineau
  • 119,623
  • 25
  • 170
  • 301
adrtam
  • 6,991
  • 2
  • 12
  • 27