I have written the following code and it's working:
#!/usr/bin/python
from os import system
import ipaddress
hostname = ipaddress.ip_address(str(input("Please Enter An IP Address : ")))
response = system("ping -c 1 " + str(hostname))
if response ==0:
print ("System is UP")
else :
print ("System is DOWN")
While running the following command, I'm getting following results which are good but I don't need any gibberish output of ping.
Please Enter An IP Address : 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=64 time=0.039 ms
--- 8.8.8.8 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.039/0.039/0.039/0.000 ms System is UP
how can a I redirect following output in some other file or in dev/null:
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=64 time=0.039 ms
--- 8.8.8.8 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.039/0.039/0.039/0.000 ms
and only receive System is UP result as output by running the script.
Please help.
system("ping -c 1 " + str(hostname) + "> /dev/null")
(by far not the best way, but the quickest)? – CristiFatios.system
, you should use the tools of thesubprocess
module: docs.python.org/3.6/library/subprocess.html It gives you a lot of control over the output. – Sven Festersen