Book Image

Python Network Programming

By : Abhishek Ratan, Eric Chou, Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker
Book Image

Python Network Programming

By: Abhishek Ratan, Eric Chou, Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker

Overview of this book

This Learning Path highlights major aspects of Python network programming such as writing simple networking clients, creating and deploying SDN and NFV systems, and extending your network with Mininet. You’ll also learn how to automate legacy and the latest network devices. As you progress through the chapters, you’ll use Python for DevOps and open source tools to test, secure, and analyze your network. Toward the end, you'll develop client-side applications, such as web API clients, email clients, SSH, and FTP, using socket programming. By the end of this Learning Path, you will have learned how to analyze a network's security vulnerabilities using advanced network packet capture and analysis techniques. This Learning Path includes content from the following Packt products: • Practical Network Automation by Abhishek Ratan • Mastering Python Networking by Eric Chou • Python Network Programming Cookbook, Second Edition by Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker
Table of Contents (30 chapters)
Title Page
Copyright
About Packt
Contributors
Preface
Index

Pinging hosts on the network with ICMP


An ICMP ping is the most common type of network scanning you have ever encountered. It is very easy to open a command-line prompt or Terminal and type ping www.google.com. How difficult is that from inside a Python program? This recipe shows you an example of a Python ping.

Getting ready

You need the superuser or administrator privilege to run this recipe on your machine.

How to do it...

You can lazily write a Python script that calls the system ping command-line tool, as follows:

import subprocess 
import shlex 
 
command_line = "ping -c 1 www.google.com" 
args = shlex.split(command_line) 
try: 
      subprocess.check_call(args,stdout=subprocess.PIPE,\ 
stderr=subprocess.PIPE) 
    print ("Google web server is up!") 
except subprocess.CalledProcessError: 
    print ("Failed to get ping.") 

However, in many circumstances, the system's ping executable may not be available or may be inaccessible. In this case, we need a pure Python script to do that ping. Note...