Pinging a machine from python

P

Prasanth

I tried pinging a machine from python using socket programming but
could not do it. Is there any module which we can use to ping the
machine < like net::ping in perl> or can you give me simple program.
 
S

Sebastian 'lunar' Wiesner

[ Prasanth said:
I tried pinging a machine from python using socket programming but
could not do it. Is there any module which we can use to ping the
machine < like net::ping in perl> or can you give me simple program.
At least on linux pinging requires raw sockets since there is no syscall for
sending ICMP packages. Raw sockets in turn require special privileges.
Therefore its best to use the os command _ping_, this limits the use of
priviledges to the required part and you don't have to deal with
priviledges (ping has suid bit set).

net::ping will most likely do the same.
 
Z

Zerge

I tried pinging a machine from python using socket programming but
could not do it. Is there any module which we can use to ping the
machine < like net::ping in perl> or can you give me simple program.

Import OS
ip=192.168.1.1
pingtext="ping "+ip+" -n 1"
pingresult=os.popen(pingtext).readlines()

"OS" gives you access to the command line of the operating system.
 
P

Prasanth

Import OS
ip=192.168.1.1
pingtext="ping "+ip+" -n 1"
pingresult=os.popen(pingtext).readlines()

"OS" gives you access to the command line of the operating system.

Thanks for the solution guys.

But the above program is giving the output as :

If we print pingresult it is giving the output as :

['\r\n', 'Pinging 0.168.1.1 with 32 bytes of data:\r\n', '\r\n',
'Request timed out.\r\n', '\r\n', 'Ping statistics for 0.168.1.1:\r
\n', ' Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),\r\n']

my requirement is

IP address and whether the machine is pinging or not. we should not
display the above output. However we can do it by using regular
expression or there is any other way. Please suggest.

Thanks,
Prasanth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,243
Latest member
CorrineCad

Latest Threads

Top