Monday, January 25, 2016

Python Connectivity Tester

To test if a connection can be established when there is no telnet available we can use this python

#!/usr/bin/env python

import socket
import sys

if len(sys.argv) < 3:
   print ("IP and PORT needed as parameters...")
   sys.exit("Exiting...")

print ('Running for:')
print ('IP:', str(sys.argv[1]))
print ('PORT:',str(sys.argv[2]))

TCP_IP = str(sys.argv[1])
TCP_PORT = int(str(sys.argv[2]))
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

print ('Connecting...')

try:
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.settimeout(5)
  s.connect((TCP_IP, TCP_PORT))
  print ("Connected")
  s.close()
            #######this is for a different python#### except socket.error, exc:
except Exception:
            #######this is for a different python#### print "Error: %s" % exc
  print ("Error: ", sys.exc_info()[1])

No comments:

Post a Comment