0

I have below function which is used check ping status

def pingOk(sHost):
 try:
 output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower()=="windows" else 'c', sHost), shell=True)
 except Exception, e:
 traceback.print_exc()
 return False
 return True

I tried to run the script

 pingOk(sHost)

I am getting below error:

Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
 [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
 [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
 [-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
 [-l preload] [-m mark] [-M pmtudisc_option]
 [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
 [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
 [-W timeout] destination
Traceback (most recent call last):
 File "set_enviroment.py", line 54, in pingOk
 output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower()=="windows" else 'c', sHost), shell=True)
 File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
 raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command 'ping -c 1 ' returned non-zero exit status 2

Looks like its not formatting output. Please someone help what i am doing mistake

asked Oct 13, 2018 at 7:45
4
  • may be due to wrong format of using ping command since its displaying ping usage help i sugget you to print the command before and validate it in terminal. Commented Oct 13, 2018 at 7:50
  • Its working fine and I tested it, it is formatting variable issue. If i use hard code values it works fine Commented Oct 13, 2018 at 7:53
  • print("ping -{} 1 {}".format('n' if platform.system().lower()=="windows" else 'c', sHost)) and check you are getting proper command Commented Oct 13, 2018 at 7:54
  • 1
    Looks like IP formatting is empty thanks Pavan Commented Oct 13, 2018 at 8:04

1 Answer 1

2

Since the complaint was about ping -c 1 , not ping -{} 1 {}, it's clear that the .format did happen.

Since the complaint was about ping -c 1 (note the trailing space), it's clear that the second argument to .format was an empty string.

The complaint that the command failed is presumably because ping expected a destination (hence all of the stderr complaining about usage).

answered Oct 13, 2018 at 7:57
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.