Telnet (NetCat) and Ping Script

Nowadays most of the Linux distros do not include Telnet. If needed we can install it using the usual methods.

Let’s embrace Netcat which is a best alternative to Telnet. Earlier I had written a Perl script which did Telnet and Ping checks across multiple hosts.

This script requires two input files ips.csv and ports.csv. As the name suggest all target host IP’s to be placed in ips.csv file and the ports to which to netcat is placed in ports.csv. Output is in HTML format and user receives an email with colour coded results.

Replace ips.csv & ports.csv file contents and change SMTP IP and email address.

Here is the Python script which replaces Telnet with Netcat.

'''
This is a python script to test connectivity between linux hosts for the specified ports. It also checks the PING status.
Netcat needs to be installed using below commands
REDHAT / CENTOS / Fedora
sudo yum -y install nc
Debian / Ubuntu / Debian Based
sudo apt install netcat
'''
import os #Native module to do file handling etc
import sys # Native module to execute sys commands
import subprocess # Execute commands to run on the OS shell
import itertools # used to iterate between two lists
import emails # Geenrate email using SMTP
from datetime import datetime # Date and time module
import logging # Default Python logging module
import shutil # shell util used here to move files
import time # Get time from system
import socket # connect to sockets
hostname = socket.gethostname() # Get Hostname
ips = open("ips.csv") # source file having list of target IP addresses
ports = open("ports.csv") # source file having list of ports to be tested
#create a log file with system date and time stamp
logfile_ = datetime.now().strftime('telnet_ping_%H_%M_%d_%m_%Y.log')
date_ = datetime.now().strftime('%H_%M_%d_%m_%Y')
timestr = time.strftime("%Y_%m_%d %H:%M:%S")
# create logger with default '__name__'
logger = logging.getLogger('Telnet_Ping')
#set logging level
#logging.basicConfig(level=logging.INFO) # Python 2.x syntax
#toggle between DEBUG and INFO to see the difference
logger.setLevel(logging.DEBUG)
#logger.setLevel(logging.INFO)

## create file handler which logs even debug messages

#Create 'logs' folder if not exists. Change the path of logdir as your git folder

logdir = "logs/"
if not os.path.exists(logdir):
os.makedirs(logdir)

fh = logging.FileHandler(logdir + logfile_)
fh.setLevel(logging.DEBUG)

# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s')
fh.setFormatter(formatter)

# add the handlers to the logger
logger.addHandler(fh)

logger.info('Telnet and Ping script started @ {}'.format(timestr))
outfile = open(hostname+".html", "w") # Output file genrated in HTML format

logger.info('Output file output.html created/Overwriting started')
outfile.write ("<table border=1>") # prepare table
outfile.write("<th colspan=4>{} Telnet & Ping Status Report - {}</th>".format(hostname,timestr))
outfile.write ("<tr><th>IP/HOST</th><th>PORT</th><th>TELNET STATUS</th><th>PING STATUS</th></tr>") # Header row

error_strings = ['refused','timed','out','0 received','100% packet loss']

for ip,port in itertools.product(ips,ports): # iterate for both IP and port
ip = ip.strip('\n') # remove new line from the IP address
logger.info('Netcat command to be executed for IP- {} PORT - '.format(ip,port))
cmd = ("nc -w 3 -zv {} {} \n".format(ip,port)) # Assign the netcat command with waiting period of 3 sec, verbose output to variable 'cmd'
p0 = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) # execute command in shell using subprocess
(output0, err0) = p0.communicate() # assign output and error to variables
output0 = output0.decode('ascii') # decode output from binary to asciii format
err0 = err0.decode('ascii')
logger.info('TELNET Status \n Output {} \n Error {} '.format(output0,err0))
#outfile.write ("<tr><td>{}</td><td>{}</td><td>{}{}</td>".format(ip,port,output0,err0)) # write the values in rows
#if "timed" in output0 or "timed" in err0:
if any (err in output0 for err in error_strings) or any (err in err0 for err in error_strings):
outfile.write ("<tr><td>{}</td><td>{}</td><td bgcolor='orange'>{}{}</td>".format(ip,port,output0,err0)) # write the values in rows
else:
outfile.write ("<tr><td>{}</td><td>{}</td><td>{}{}</td>".format(ip,port,output0,err0))
cmd1 = ("ping -c 3 {}".format(ip)) # Assign the ping command with timeout 3 sec
logger.info('Ping command to be executed for IP-{}'.format(ip))
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output1, err1) = p1.communicate()
output1 = output1.decode('ascii')
err1 = err1.decode('ascii')
logger.info('PING Status \n Output {} \n Error {} '.format(output1,err1))
#err1 = p1.stderr.read()
if any (err in output1 for err in error_strings) or any (err in err1 for err in error_strings):
outfile.write ("<td bgcolor='orange'>{}{}</td>".format(output1,err1))
else:
outfile.write ("<td>{}{}</td>".format(output1,err1))

#outfile.write ("<td>{}{}</td>".format(output1,err1))
outfile.write ("</table>")# close the table
outfile.close()


#Generate Email
logger.info("Generate Email")
message = emails.html(html="<p>Hi,<br>Please find the Telnet and Ping Status Report<br />" ,
subject="%s Telnet & Ping Status Report at %s EST" % (hostname,timestr),
mail_from=('telnet_ping_report'))
message.attach(data=open('%s.html' % (hostname), 'rb'), filename='%s.html'%(hostname))

r=message.send(to=['vinay.umesh@abc.com'], smtp={'host': '10.10.1.5', 'timeout': 5})

#Validate email sent to recepients or not
assert r.status_code == 250
if r.status_code == 250:
print("Summary Email sent successfully")
logger.info("Summary Email sent successfully")
else:
print("Summary Email failed to send. Returncode - {}".format(status_code))
logger.error("Summary Email failed to send. Returncode - {}".format(status_code))

What am I missing here? Let me know in the comments and I’ll add it in! Give it a try and I’m sure this will be a handy tool when you’re facing a connectivity issue.

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s