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.
Related
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.
I am trying to telnet into a cisco ios-xr router and gather command output.
I have tested that the below code successfully connects to the router and executes the command, however it seems that neither print tn.read_all() nor tn.read_very_eager() works. They do not print anything. Am I missing anything obvious here?
#!/usr/bin/env python
import sys
import telnetlib
import time
HOST = "10.62.53.34"
PORT = "17006"
user = "cisco"
password = "cisco"
tn = telnetlib.Telnet(HOST,PORT)
print "Telnetting to", HOST, "#",PORT
tn.write("\n")
tn.write(user + "\n")
tn.write(password + "\n")
#print("I am in")
tn.write("show runn\n")
tn.write("exit \n")
print tn.read_all()
tn.close()
though this question is from february, i'll post my answer for a potential future googler.
i fixed a similar problem when i realized that:
print tn.read_all()
...is valid in python 2 (see the example at the bottom of https://docs.python.org/2/library/telnetlib.html), but not in python 3 (https://docs.python.org/3.6/library/telnetlib.html).
for python 3, the correct syntax would be:
print(tn.read_all())
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.
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.
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()