2

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...

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Seth
  • 103
  • 1
  • 2
  • 14
  • From where does the input end up in Notepad++?` – tripleee Apr 30 '14 at 04:36
  • I open the file pingresults.txt with notepad++ – Seth Apr 30 '14 at 04:37
  • You're getting back bytes (that's what the `b'...'`) means so you've got to DECODE said bytes on input to your program and ENCODE them on the way out – mechanical_meat Apr 30 '14 at 04:52
  • Yes the strip is doing something but removing that doesn't help. When I try the suggestion below I get an error I can't concat byte to string – Seth Apr 30 '14 at 05:03
  • OK my problem is the wite is writing everything in one line. I thought it was looping as the for line in line went but it is not. The text is written all as one line in the file so adding a os.linesep just sticks one at the very end of the line of text. How do I break each line retuened by ping.communicate – Seth Apr 30 '14 at 05:24
  • Duplicate question even answer here. Error in answer though stderr.subprocess.PIPE is what it should be not Popen http://stackoverflow.com/questions/16862111/python-console-and-text-output-from-ping-including-n-r – Seth Apr 30 '14 at 06:32

1 Answers1

3

I checked this code again on python 2.7 on ubuntu and it does work fine with the addition of the '\n' or os.linesep.

On python 3.2, subprocess.communicate returns a byte array.

using out.decode() should convert it to a string.

[python 2.7 below]

string.strip() removes end of line characters by default.

"Hello\n".strip()

returns

'Hello'

use

output.write(str(out + '\n'))

or

output.write(str(out + os.linesep))

[Python 3.X below]

output.write(out.decode() + os.linesep)
rkh
  • 1,761
  • 1
  • 20
  • 30