I am a total noob to python. But have lots of CLI experience on Cisco and such. I have decided to make Python my 'go to' script tool. And well, I have failed miserably my first try even stealing most of the code...
I am doing a ping of a list of hosts. The purpose is to 1. Validate it is alive and 2. get the IP
The script I got online worked after a bit of tweaking for Version 3.4.
The display shows perfect like I want. The file it writes is just one long line which appears to have the \r\n just written as part of the text. This is the code and a sample of the screen dispay and the written file.
import sys
import os
import platform
import subprocess
import threading
import pexpect
hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()
for line in lines:
line = line.strip()
ping = subprocess.Popen(["ping", "-n", "3",line],stdout = subprocess.PIPE,stderr = subprocess.PIPE)
out, error = ping.communicate()
out = out.strip()
error = error.strip()
output = open("PingResults.txt",'a')
output.write(str(out))
output.write(str(error))
print(out.decode('utf-8'))
print(error.decode('utf-8'))
hosts_file.close()
Screen is perfect
Pinging HOST7.foo [10.180.43.209] with 32 bytes of data:
Reply from 10.180.43.209: bytes=32 time=81ms TTL=60
Reply from 10.180.43.209: bytes=32 time=56ms TTL=60
Reply from 10.180.43.209: bytes=32 time=56ms TTL=60
Notepad++ reads as single line with visible \r\n (as do other editors
b'Pinging host7.foo [10.180.43.11] with 32 bytes of data:\r\nReply from 10.18.43.11: bytes=32 time=555ms TTL=60\r\nReply from 10.18.43.11: bytes=32 time=140ms TTL=60\r\nReply from 10.180.43.11: bytes=32 time=139ms TTL=60\r\n\r\nPing statistics for 10.180.43.11:\r\n Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),\r\nApproximate round trip times in milli-seconds:\r\n Minimum = 139ms, Maximum = 555ms, Average = 278ms'b''b'Pinging host9.foo [10.180.43.25] with 32 bytes of data:\r\nReply from
Help me Obi-Wan Kenobie...you're my only hope...