lost and found ( for me ? )

Python : TCP health monitoring with socket module

Here is a TCP health monitoring script with socket module.
$ cat tcp3way.py
#!/usr/bin/env python

import sys
import socket

argvs = sys.argv
argc = len(argvs)

def client(host, port):
   try:
       s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       port = int(argvs[2])
       s.connect((host, port))
       #print "success"
       return True
   except:
       #print "can't connect"
       return False
# close a socket
   finally:
       #print "close a socket"
       s.close()

if __name__ == "__main__":
   if argc == 3:
       result = client(argvs[1], argvs[2])
       if result == True:
           print "can connect to %s:%s" % (argvs[1], argvs[2])
       else:
           print "can not connect to %s:%s" % (argvs[1], argvs[2])
           
   else:
       print("usage python tcp3way.py IPaddress PORT")


$ python tcp3way.py 192.168.11.1 80
can connect to 192.168.11.1:80

Here is a capture data.
 1   0.000000 192.168.11.100 -> 192.168.11.1 TCP 74 53387 > 80 [SYN] Seq=0 Win=29200 Len=0 MSS=1460 SACK_PERM=1 TSval=1355687 TSecr=0 WS=128
1   2   0.999319 192.168.11.100 -> 192.168.11.1 TCP 74 [TCP Retransmission] 53387 > 80 [SYN] Seq=0 Win=29200 Len=0 MSS=1460 SACK_PERM=1 TSval=1355937 TSecr=0 WS=128
2   3   1.003364 192.168.11.1 -> 192.168.11.100 TCP 74 80 > 53387 [SYN, ACK] Seq=0 Ack=1 Win=5792 Len=0 MSS=1460 SACK_PERM=1 TSval=531209006 TSecr=1355937 WS=2
 4   1.003462 192.168.11.100 -> 192.168.11.1 TCP 66 53387 > 80 [ACK] Seq=1 Ack=1 Win=29312 Len=0 TSval=1355938 TSecr=531209006
 5   1.003603 192.168.11.100 -> 192.168.11.1 TCP 66 53387 > 80 [FIN, ACK] Seq=1 Ack=1 Win=29312 Len=0 TSval=1355938 TSecr=531209006
 6   1.005485 192.168.11.1 -> 192.168.11.100 TCP 66 80 > 53387 [ACK] Seq=1 Ack=2 Win=5792 Len=0 TSval=531209007 TSecr=1355938
 7   1.183689 192.168.11.1 -> 192.168.11.100 TCP 66 80 > 53387 [FIN, ACK] Seq=1 Ack=2 Win=5792 Len=0 TSval=531209039 TSecr=1355938
 8   1.183760 192.168.11.100 -> 192.168.11.1 TCP 66 53387 > 80 [ACK] Seq=2 Ack=2 Win=29312 Len=0 TSval=1355983 TSecr=531209039


$ python tcp3way.py 192.168.11.1 8080
can not connect to 192.168.11.1:8080

capture data
 1   0.000000 192.168.11.100 -> 192.168.11.1 TCP 74 58051 > 8080 [SYN] Seq=0 Win=29200 Len=0 MSS=1460 SACK_PERM=1 TSval=1370632 TSecr=0 WS=128
 2   0.004244 192.168.11.1 -> 192.168.11.100 TCP 54 8080 > 58051 [RST, ACK] Seq=1 Ack=1 Win=0 Len=0

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.