Pinging a machine from python

Prasanth prasanths89 at gmail.com
Mon Jul 21 21:50:58 EDT 2008


On May 26, 5:21 am, Zerge <zerg... at gmail.com> wrote:
> On May 25, 11:13 am, Prasanth <prasanth... at gmail.com> wrote:
>
> > 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.

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.



More information about the Python-list mailing list