Nmap 스캔의 출력을 파이썬을 사용하여 텍스트 파일로 리디렉션하려고합니다.서브 프로세스의 출력을 파일로 리디렉션
outputName = raw_input("What is the output file name?")
fname = outputName
with open(fname, 'w') as fout:
fout.write('')
command = raw_input("Please enter an Nmap command with an IP address.")
args = shlex.split(command)
proc = subprocess.Popen(args,stdout=fname)
오류 : 워드 프로세서
Traceback (most recent call last):
File "mod2hw4.py", line 17, in <module>
proc = subprocess.Popen(args,stdout=fname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 701, in __init__
errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1127, in _get_handles
c2pwrite = stdout.fileno()
AttributeError: 'str' object has no attribute 'fileno'
감사합니다.이 또한 도움이되었습니다. –
주 :'Popen'이 자식 프로세스가 종료되기를 기다리지 않기 때문에 * nmap이 끝나기 전에 파일이 부모 프로세스에서 닫힐 수 있습니다. nmap은 파일 디스크립터의 복사본을 상속 받는다. – jfs
@ J.F.Sebastian이 맞습니다. 프로세스가 먼저 완료 될 때까지 명시 적으로 기다려야합니다. with 블록에서이를 수행하기 위해'subprocess.wait'에 대한 호출을 추가했습니다. – wrigby