Pinging a machine from python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prasanth

    Pinging a machine from python

    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.
  • Sebastian 'lunar' Wiesner

    #2
    Re: Pinging a machine from python

    [ Prasanth <prasanths89@gm ail.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)

    Comment

    • Zerge

      #3
      Re: Pinging a machine from python

      On May 25, 11:13 am, Prasanth <prasanth...@gm ail.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.p open(pingtext). readlines()

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

      Comment

      • Prasanth

        #4
        Re: Pinging a machine from python

        On May 26, 5:21 am, Zerge <zerg...@gmail. comwrote:
        On May 25, 11:13 am, Prasanth <prasanth...@gm ail.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.p open(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.

        Comment

        Working...