Python socket works as a telnet connection and requires double read - python

I'm connecting to the device using telnet. Once I connected, I need to enter user name admin, press enter \n and use blank password (enter again \n). After the described login process I should end up with the invitation (prompt) #. I'm reading from the socket after each write. The program is below:
import socket
s = socket.socket()
#Connecting using telnet
s.connect(('192.168.1.2',23))
a = s.recv(1000)
print 'a:', a
s.send('admin\n\n')
b = s.recv(1000)
print 'b:', b
c = s.recv(1000)
print 'c:', c
The output of the program is the following:
a: ��
b: �������� ��!��"��'����#��User:
c: admin
Password:
(prompt) #
QUESTION:
Why do I need to do double read to receive all the information (print b and print c) instead of just one read (print b)?

Instead of using raw sockets, you can use the telnetlib.
Typical usage:
import getpass
import sys
import telnetlib
HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
This will ask for your username and password, then use the entered information to authenticate to the server as seen here:
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
You can read the telnetlib documentation for python 2.x here.

Related

How can i get telnet output printed line by line?

I have implemented a script that connects to a Cisco device and collects the show run output.
The script is:
import getpass
import telnetlib
import time
HOST = "10.62.149.9"
user = input("Enter your remote account: ")
password = getpass.getpass("User Password: ")
enable = getpass.getpass("Enable Password: ")
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"enable\n")
if enable:
tn.read_until(b"Password: ")
tn.write(enable.encode('ascii') + b"\n")
tn.write(b"term len 0\n")
tn.write(b"show run\n")
time.sleep(10)
s = tn.read_very_eager()
print (s)
tn.close()
The script output is:
\r\ninterface GigabitEthernet1/0/26\r\n!\r\ninterface GigabitEthernet1/0/27\r\n!\r\ninterface GigabitEthernet1/0/28\r\n!\r\ninterface GigabitEthernet1/0/29\r\n!\r\ninterface GigabitEthernet1/0/30\r\n!\r\ninterface GigabitEthernet1/0/31\r\n!\r\ninterface GigabitEthernet1/0/32\r\n!\r\ninterface GigabitEthernet1/0/33\r\n!\r\ninterface GigabitEthernet1/0/34\r\n!\r\ninterface GigabitEthernet1/0/35\r\n!\r\ninterface GigabitEthernet1/0/36\r\n!\r\ninterface GigabitEthernet1/0/37\r\n!\r\ninterface GigabitEthernet1/0/38\r\n!\r\ninterface GigabitEthernet1/0/39\r\n!\r\ninterface GigabitEthernet1/0/40\r\n!\r\ninterface GigabitEthernet1/0/41\r\n!\r\ninterface GigabitEthernet1/0/42\r\n!\r\ninterface GigabitEthernet1/0/43\r\n!\r\ninterface GigabitEthernet1/0/44\r\n!\r\ninterface GigabitEthernet1/0/45\r\n!\r\ninterface GigabitEthernet1/0/46\r\n!\r\ninterface GigabitEthernet1/0/47\r\n!\r\ninterface GigabitEthernet1/0/48\r\n no switchport\r\n ip address 10.62.149.9
255.255.255.128\r\n!\r\ninterface GigabitEthernet1/0/49\r\n!\r\ninterface GigabitEthernet1/0/50\r\n!\r\ninterface GigabitEthernet1/0/51\r\n!\r\ninterface GigabitEthernet1/0/52\r\n!\r\ninterface Vlan1\r\n no ip address\r\n shutdown\r\n!\r\ninterface Vlan10\r\n ip address 10.10.10.1
255.255.255.0\r\n!\r\ninterface Vlan199\r\n ip address 217.21.0.6 255.255.254.0\r\n no ip redirects\r\n ip ospf priority 100\r\n!\r\ninterface Vlan777\r\n ip address 7.7.7.1
Question
Is there a way to change the script so the output is presented line by line way?
The documentation for telnetlib isn't very clear about Telnet.read_very_eager, but it mentions that it reads and writes bytes for other methods. type(s) is probably bytes.
Use bytes.decode to get a string: print(s.decode('ascii')) (use the appropriate encoding)
Note: This is for Python 3.
You need to check the string with some common special character that separates the lines using python split() function and after that initiate a for loop with the length of variable and then print.

telnet with python - no promt after password input

Im trying to telnet to a NETGEAR SWITCH through python script,
i succeed to connect through Putty, but when im trying with python code
i get nothing (no promt) after putting the password (which is empty, only need to press enter.
Here is the code:
import getpass
import telnetlib
import time
HOST = "10.10.10.1"
user = input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"User: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"ls\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
I tried to put sleep delays and double checked the read_until parameters and also with and without spaces, but with no success,
please help.

Telnet script does not work inside a function

Out of function, the script works, but inside the function, script does not work.
import telnetlib
import sys
def teltest():
host = "192.168.2.2"
user = "admin"
password = "admin"
tn = telnetlib.Telnet(host)
tn.read_until("Username:")
tn.write(user + "\n")
tn.read_until("Password:")
tn.write(password + "\n")
tn.write("enable\n")
tn.write("config t\n")
tn.write("interface eth 0/0/13\n")
tn.write("description TEST\n")
teltest()
Why and how can i fix it?
This is happening because of the function returns before properly terminating the connection leaving the other end of the device in a meta state. Adding a sleep at the end as mentioned in the comments would let room for cleaning up the connection thereby executing what was written onto the device.
Telnet.write(buffer) Write a string to the socket, doubling any IAC
characters. This can block if the connection is blocked. May raise
socket.error if the connection is closed.
import telnetlib
import sys
def teltest():
host = "192.168.2.2"
user = "admin"
password = "admin"
tn = telnetlib.Telnet(host)
tn.read_until("Username:")
tn.write(user + "\n")
tn.read_until("Password:")
tn.write(password + "\n")
tn.write("enable\n")
tn.write("config t\n")
tn.write("interface eth 0/0/13\n")
tn.write("description TEST\n")
time.sleep(1)
teltest()
Posting this as an answer for the benefit of the community despite the fact that the op received help from comments.

Telnet from inside a telnet session (Python)

I am trying to telnet to a remote device from another remote device, doing a nested telnet using telnetlib. While I can easily communicate with the first device, I am not able to get the output from the second device. Below is my code, am I doing this correctly?
import telnetlib
HOST = "firstDevice"
user = "lab"
password = "lab"
tn = telnetlib.Telnet(HOST)
tn.read_until("Username: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
#Nested telnet
tn2 = telnetlib.Telnet("secondDevice")
tn2.read_until("#")
tn2.write("sh clock\n")
#Close tn2
tn2.write("exit\n")
print tn2.read_all()
#Close tn
tn.write("exit\n")
print tn.read_all()
Edit 1
import telnetlib
HOST = "firstDevice"
user = "lab"
password = "lab"
tn = telnetlib.Telnet(HOST)
tn.read_until("Username: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
#Nested telnet
tn.write("telnet secondDevice\n")
tn.write("sh clock\n")
#Close nested session
tn.write("exit\n")
#Close tn
tn.write("exit\n")
print tn.read_all()
You are not doing a nested connection in your code. You are just connecting to two different computers from localhost, but apparently you can not actually connect to the second one. To do a nested Telnet to the second host, you have to tell the first one to telnet to the second one: replace tn2 = telnetlib.Telnet("secondDevice") with
tn.write("telnet secondDevice\n")
Since you have a nested connection, all your localhost should see is tn. You can get rid of the tn2 object entirely. All interaction with the second device will be done by sending strings to the first device, which is in a session connected to the second device.

Qustion regarding telnet to a switch on the network and sending in specific commands

I have written a python program to telnet to a switch on the network, login and enter specific commands. But I don't know what is wrong with my program as the program never gets passed through after login. I doubt the program is even taking in the password I gave at command line.
Here is my program:
import telnetlib
import getpass
import sys
Host = "10.210.1.2"
user = raw_input("hbommireddy")
password = getpass.getpass()
tn = telnetlib.Telnet(Host)
tn.read_until("Login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("SYSTEM-QA-S4810>")
tn.write("en\n")
tn.read_until("SYSTEM-QA-S4810#")
tn.write("show interface status\n")
print tn.read_all()

Categories

Resources