I needed to do a minimal tcp server to check if I was able to connect to that host from another machine. I found out that I did not have any nc (NetCat) available on the machine, and no working repo for the machine either.
So I wanted to setup a small tcp listener that wasn’t nc based. I guess I could have used nc -l 443 for the same task if it was available on the system.
1234567891011121314151617181920212223242526272829
import socket
import sys
HOST = '' # Symbolic name, meaning all available interfaces
PORT = 443 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
s.close()
Setup of port knocking to let networking guys get something to sniff for while I do other stuff:
123456789101112131415
[user@host1 ~]$ while true; do date ; echo trying... ; nc imap.mailserver.tld 143 -w 10; sleep 50; done
Tue Oct 3 08:53:10 CEST 2017
trying...
Tue Oct 3 08:54:10 CEST 2017
trying...
Tue Oct 3 08:55:10 CEST 2017
trying...
Tue Oct 3 08:56:10 CEST 2017
trying...
Tue Oct 3 08:57:10 CEST 2017
trying...
Tue Oct 3 08:58:10 CEST 2017
trying...
Tue Oct 3 08:59:10 CEST 2017
trying...