KB: Replace telnet with a python script
On some editor's Linux based appliance(s), there is not telnet binary installed. To test TCP netwotk flows, it's possible to use python's socket module with a short script:
1#!/usr/bin/python
2
3import socket
4import sys
5
6if len(sys.argv) != 3:
7 print("usage: telnet.py IPADDRESS PORT")
8 exit(-1)
9
10print("Opening connection on %s port %s" % (sys.argv[1], sys.argv[2]))
11
12try:
13 conn=socket.create_connection((sys.argv[1],sys.argv[2]),timeout=30)
14except socket.timeout:
15 print("Connection error: timeout")
16 exit(-1)
17except:
18 print("Connection error: unknown")
19 exit(-1)
20print("Connection succeed")
21exit(0)
Usage:
1$ python telnet.py
2usage: telnet.py IPADDRESS PORT
✅ Successful test:
1$ python telnet.py 10.10.10.10 443
2Opening connection on 10.10.10.10 port 443
3Connection succeed
❌ Failed test:
1$ python telnet.py 10.10.10.10 10443
2Opening connection on 10.10.10.10 port 10443
3Connection error: unknown