简体   繁体   中英

Python / Paramiko How do I interpret ping results.

Here is my code

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='usrname', password='pwd)

stdin, stdout, stderr = ssh.exec_command("vmkping ip")
output = stdout.readlines()

I want to be able to tell what percent of the packets were received. I also want to tell if there was an error when pinging. The output variable only stores the results as a list of strings.

You can process the output using re and string functions and get the required output.

Specify how many pings should be attempted in the ping command(Here i have mentioned a static value):

command = "ping -c 5 <ip>"
ssh_stdin, ssh_stdout, ssh_stderr = ssh_connection.exec_command(command)

output = ssh_stdout.read()
error = ssh_stderr.read()

list_output = re.split(',', output)
packet_loss = re.split('packet loss', list_output[2])
print packet_loss[0]

To process the output for all types of errors, you can do something like:

if str(output).find('5 received') > 0:
   print "5 packets received"
elif str(output).find('0 received') > 0:
   print "0 packet received"

Similarly you can process the output and find out what percent of packets received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM