I wanted to collect the IP addresses of a number of hostnames, and I thought I'd do it by using the Windows Command Prompt to ping each host name just once, then collect my results (I did not want 4 pings to each hostname). When I tried to run 'ping -c 1 myhostname' I got an error regards it needs administrative privileges (which I don't have on the enterprise desktop I am using.)
I have Python on my Enterprise desktop and came across this:
https://stackoverflow.com/questions/2953462/pinging-servers-in-python
And it works like ping -c 1!
import platform # For getting the operating system name
import subprocess # For executing a shell command
def ping(host):
# Option for the number of packets
param = '-n' if platform.system().lower()=='windows' else '-c'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host]
return subprocess.call(command) == 0
And on reading the Python, I realized that if I'd used 'ping -n 1 myhostname' from the Windows Command Prompt, I would not have needed Python. You live and learn! The image below shows the 'Access denied. Option -c requires administrative privileges' and doing ping -n 1 myhostname.
Image: How to do just one ping in the Windows Command Prompt (use ping -n)
Comments
Post a Comment