By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Pinging a machine from python

P: n/a
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 perlor can you give me simple program.
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
[ Prasanth <pr*********@gmail.com]
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 perlor 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.

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jun 27 '08 #2

P: n/a
On May 25, 11:13 am, Prasanth <prasanth...@gmail.comwrote:
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 perlor 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.
Jun 27 '08 #3

P: n/a
On May 26, 5:21*am, Zerge <zerg...@gmail.comwrote:
On May 25, 11:13 am, Prasanth <prasanth...@gmail.comwrote:
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 perlor 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.
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.
Jul 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.