Bloggen min

Litt om min daglige koding

Debugging Network Connection/firewalls

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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...