我对我的脚本有两个问题,如何将其输出到我请求的文件中。我问这个因为它无限循环,当我取消脚本并显示文件时它是空的。另外,如果必须取消脚本输入任何内容,如何使用分配的变量?谢谢!
import subprocess
import datetime
#open results file and assign to results variable, add append rights
results = open("results.txt", "a")
#Run until stopped
while 1:
#split the blah variable by line
#Run tshark command 100 times, then restart script. Assign to blah variable
blah = subprocess.check_output(["tshark -i mon0 -f \"subtype probe-req\" -T fields -e wlan.sa -e wlan_mgt.ssid -c 20"], shell=True)
splitblah = blah.split("\n")
#repeat for each line, ignore first line since it contains headers
for value in splitblah[:-1]:
#split each line by tab delimiter
splitvalue = value.split("\t")
#Assign variables to split fields
MAC = str(splitvalue[1])
SSID = str(splitvalue[2])
time = str(datetime.datetime.now())
#write and format output to results file
Results.write(MAC+" "+SSID+" "+time+"\r\n")
答案 0 :(得分:1)
你应该在你的while
陈述中加上一个条件,否则程序(确实)永远不会停止。
此外,在someFileObject.write
函数调用后,数据不一定立即写在磁盘上,您需要调用someFileObject.flush
来确保数据。