Use different IP on telnetlib.Telnet, instead of chosen by default one?

751 views Asked by At

I need to make a telnet conversation and this brought me to use official telnetlib library, widely explained here: telnetlib. The machine that actually use this software has several IPs, but it seems telnetlib does not provide an easy - or evident, at least - way to change my IP used in the handshake phase of telnet conversation.

Anyone has some useful information about that?

1

There are 1 answers

1
Alex G Rice On BEST ANSWER

The source to telnetlib is here, and it appears it does not provide any way to specify the IP of the socket, leaving it up to the OS to select, which will result in the 'default' network interface.

self.sock = socket.create_connection((host, port), timeout)

https://hg.python.org/cpython/file/2.7/Lib/telnetlib.py#l227

The create_connection() signature is:

socket.create_connection(address[, timeout[, source_address]])

If supplied, source_address must be a 2-tuple (host, port) for the socket to bind to as its source address before connecting. If host or port are ‘’ or 0 respectively the OS default behavior will be used. https://docs.python.org/2/library/socket.html

You could copy the telnetlib.py and modify the create_connection()

I have not tested this! But it could be like this on l227

self.sock = socket.create_connection((host, port), timeout, (src_host, src_port))

or even add a keyword arguments to the open() call to pass in the desired interface info.