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
Dev Ops Ranga
2136 silver badges15 bronze badges
-
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.Pavan Kumar T S– Pavan Kumar T S2018年10月13日 07:50:34 +00:00Commented 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 fineDev Ops Ranga– Dev Ops Ranga2018年10月13日 07:53:29 +00:00Commented 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 commandPavan Kumar T S– Pavan Kumar T S2018年10月13日 07:54:49 +00:00Commented Oct 13, 2018 at 7:54
-
1Looks like IP formatting is empty thanks PavanDev Ops Ranga– Dev Ops Ranga2018年10月13日 08:04:38 +00:00Commented Oct 13, 2018 at 8:04
1 Answer 1
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
torek
500k71 gold badges765 silver badges892 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py