我正在研究从CSV ping IP的代码。这时,程序将读取csv,根据网关生成要ping的IP,并为应进行ping的每个设备将IP提高1。这来自用户输入,该输入指定要ping的设备数。该程序将ping操作写入txt文件:
PING 172.217.10.143 (172.217.10.143): 56 data bytes
64 bytes from 172.217.10.143: icmp_seq=0 ttl=53 time=27.403 ms
64 bytes from 172.217.10.143: icmp_seq=1 ttl=53 time=26.831 ms
64 bytes from 172.217.10.143: icmp_seq=2 ttl=53 time=26.835 ms
64 bytes from 172.217.10.143: icmp_seq=3 ttl=53 time=26.801 ms
64 bytes from 172.217.10.143: icmp_seq=4 ttl=53 time=26.753 ms
--- 172.217.10.143 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max/stddev = 26.753/26.925/27.403/0.241 ms
我正在尝试对此进行解析,并生成一个将存储数据的csv文件。这可以用python完成吗?我可以直接将此数据发送到csv以启动吗?
我如何将数据获取到txt文件:
def ping(ip, count):
if (count != 0):
pingResults = open('Results.txt', 'a')
toPing = subprocess.Popen(["ping", "-c", count, ip], stdout =
subprocess.PIPE)
for line in toPing.stdout:
pingResults.write(line.decode('utf-8'))
toPing.wait()
答案 0 :(得分:0)
这是一个简单的Python脚本,可将Ping输出格式转换为包含两列的CSV:IP地址和响应延迟
with open('captured_ping_output.txt') as file_handle:
file_content = file_handle.read()
with open('formatted_ping_responses.csv') as file_handle:
for line in file_content.split('\n'):
if line.startswith('64 bytes'):
line_as_list = line.split(' ')
what_to_write = line_as_list[3].strip(':')+", "+
line_as_list[6].split('=')[1]
print(what_to_write)
file_handle.write(what_to_write)