Ping Using Python Script

January 18, 2017 | Views: 73283

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Estimated reading time: 1 minute

Hello Cybrary Community,

This is my first little article on Cybrary, and as the title says, I’ll show you how to create your own Ping tool with a simple script using Python,

So, what is Ping?

According to WikiBooks, “Ping is a basic Internet tool that allows a user to verify that a particular IP address exists and can accept requests.”

Let’s start writing our script for Ping:

#######################################

  • import socket
  • import sys
  • s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #Create a TCP/IP socket
  • server_ip = raw_input(‘Enter server IP : ‘)
  • rep = os.system(‘ping ‘ + server_ip)
  • if rep == 0:
    • print ‘n n server is up n n’
  • else:
    • print ‘server is down’

#######################################

That’s it! See, I told you it was simple. Happy Pinging 🙂

Comment below if you have any questions, and thanks.

Start Learning Cyber Security with these FREE Courses:

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
17 Comments
  1. It is bullshit. You reused simple command line ping. If I remove ping.exe from system your script would fail. If you name it a python pinger, use raw sockets and not just import socket library.

    • without accessing the socket, why do you need to import the socket Library.As well, ping through command line only. could you make a script for pure python if possible.

  2. from os import system
    print ‘Server is down’ if system(‘ping ‘ + raw_input(‘Enter server address: ‘)) else ‘Server is up’

    I made it more concise.

  3. what if you were trying to ping a computer on your LAN.insted of server ip would it be the client pc.?

    • The variable ‘server_ip’ could just as simply be described as ‘ip’. An IP describes a desgination host – whether the ping command routes the ICMP traffic internally on the LAN, or destines it for the source host’s gateway – is largely dependent upon network conditions (network-level/host-level firewalls, network interfacing / ARP table entries, etc.)

  4. Guys, I am not a python expert but I have been programming in C++ for quite some time … why to create a socket if you are not using it ?
    Try to comment line 3 and the “script” will work … what the script do in line 6 is calling the OS command “ping” …
    Amine, this is not “pinging” with python but just “wrapping” ping within a script …

    • You’re right, the socket is not used. There is no padding, IP/ICMP packet construction, or anything along these lines. You only need to import ‘os’ and use “os.system(‘ping ‘ + ip)”.

Page 2 of 2«12
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel