import getpass
import sys
import telnetlib
tn = telnetlib.Telnet("xxxxxxxx")
tn.write("xxxxxx" + "\n")
tn.write("xxxxxx" + "\n")
tn.write("show version\n")
tn.write("exit\n")
print tn.read_all()
Trying to telnet to a router and the script is hanging.... Not sure whats up. Have tried a debug but cant make head or toe of the output.
telnetlib.py(511): if self.irawq >= len(self.rawq):
telnetlib.py(512): self.rawq = ''
telnetlib.py(513): self.irawq = 0
telnetlib.py(516): buf = self.sock.recv(50)
This is where it hangs in the debug.
Thanks
William
I can answer this one. Managed to get it fixed.
The python script was going off the rails when the router asked for user input to see more information on the command.
So when i asked for a "show version" it would give out a lot of information onto the command line and then ask you hit space to see more.
To solve this the first command i execute is "term len 0". Stops it asking for any user input and just fires it all out at once.
below is the final script. Changed quite a bit but its works.
import sys
import telnetlib
tn = telnetlib.Telnet("xxxxxxxx")
TELNET_PROMPT="xxxxxxxxxx"
TIMEOUT=1
tn.write("xxxxxxx"+"\n")
tn.write("xxxxxxx"+"\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("term len 0" + "\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("show version" + "\n")
print tn.read_until(TELNET_PROMPT, TIMEOUT)
tn.write("exit"+"\n")
tn.close()
Related
may be this question was asked already but i cant find answer what works for me so please need your help . I try write script what automaticly will start another script if service down, in my case apache2 , i write small script but i cant make it work its not recognize output , please your help , here the script :
import os
import subprocess
service = "apache2"
p = subprocess.Popen(["systemctl", "is-active", service], stdout=subprocess.PIPE)
(output, err) = p.communicate()
output = output.decode('utf-8')
print 'APA Serv :',(output)
if output == "inactive":
print '\x1b[31;1m' + ' Attention!' + '\x1b[0m'
os.system('sudo python baz.py')
time.sleep(2)
p.stdout.close()
I have some problems with the python libary telnetlib. Login works. Enable, ? and Exit works. But all commands with an space doesn't work.
import getpass
import sys
import telnetlib
HOST = "10.159.123.91"
user = "1234"
command = "show version"
tn = telnetlib.Telnet(HOST)
tn.read_until("Password: ")
tn.write(user + "\n")
tn.write("enable\n")
tn.write("5678\n")
tn.write("?\n")
tn.write("exit\n")
print tn.read_all()
tn.close()
This one is working. If I replace tn.write("?\n") with tn.write("show version\n") nothing happen.
Does anyone have any idea where my problem is?
I found an solution. I'm not sure if it is an good and clean solution. But it works. I add behind the command an sleep for 2 seconds.
import getpass
import sys
import time
import telnetlib
HOST = "10.159.123.91"
user = "1234"
command = "show version"
tn = telnetlib.Telnet(HOST)
tn.read_until("Password: ")
tn.write(user + "\n")
tn.write("enable\n")
tn.write("5678\n")
tn.write("show ip interface brief\n")
time.sleep(2)
tn.write("exit\n")
print tn.read_all()
tn.close()
I have written a small Python code to test it for a bigger project. The subprocess works great if everything's fine. But I want to parse the output error message for particular errors so I can figure out what the error is such as- IP not in network, or wrong username, wrong password.
Even the print at the end doesnt work. My interest in however not to print those. I was merely checking if the parsing works!
import sys
import subprocess
import os
ip = "192.168.1.20"
password = "password"
username = "sam"
ans = subprocess.check_output(['sudo','xfreerdp','-p',password, ip])
for line in ans.split('\n'):
if "protocol security negotiation" in line:
print "Not Windows!"
print "JUST TO test if this print works and it doesnt!!"
Try using try..except
import sys
import subprocess
import os
ip = "192.168.1.20"
password = "password"
username = "sam"
try:
ans = subprocess.check_output(['sudo','xfreerdp','-p',password, ip])
except subprocess.CalledProcessError as e:
ans = e.output
for line in ans.split('\n'):
if "protocol security negotiation" in line:
print "Not Windows!"
print "JUST TO test if this print works and it doesnt!!"
You need to redirect STDERR in order to check messages printed to it. Add:
subprocess.check_output(['sudo','xfreerdp','-p',password, ip], stderr=subprocess.STDOUT)
So I have this script I can't get working. I have verified it's the script, as I can cmd->telnet to localhost 4212 and are able to change vlc there(like next and stop).
But the script doesn't give any errors either. I have python installed:
#!/usr/bin/python
import sys
import telnetlib
cmd = " ".join(sys.argv[1:])
tn = telnetlib.Telnet("localhost", 4212)
tn.read_until("Password: ")
tn.write("admin\n")
tn.read_until("> ")
tn.write(cmd + "\n")
tn.close()
So I'm trying this really simple example given by the python docs:
import getpass
import sys
import telnetlib
HOST = "<HOST_IP>"
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()
My issue is that it hangs at the end of the read_all()... It doesn't print anything out. I've never used this module before so I'm trying to get this really basic example to work before continuing. BTW, I'm using python 2.4
Thank you.
Okay, I found a solution. Before I entered ls and exit, I needed to first specify the terminal type. Adding
tn.write("vt100\n")
before the "ls" fixed the problem for me.
If you're using Windows, be sure to add carriage return (\r) before the new line character:
tn.write(user.encode('ascii') + "\r\n".encode('ascii'))
I know this is late to post but may help others. I also struggled to get this right but here is my piece of code. My telnet would follow certain flow like it would ask for loginID and then Password and then you have to wait for a particular string to be displayed here,which for my case was "DB>" then you can proceed with the command and all. My output would be saved in "out" varible
import os,re,telnetlib
host = "10.xxx.xxx.xxx"
port = 23
telnet = telnetlib.Telnet()
telnet.open(host, port)
telnet.write('loginID\r\n')
telnet.write('Password\r\n')
out = telnet.read_until("DB>", 5)
telnet.write('show cable modem reg\r\n') #Mycommand
out = telnet.read_until("DB>", 5)
telnet.write('quit\r\n')
telnet.close()
For more variations and help, Check the website nullege
I don't have a telnet server to test against, but I think the issue is that you are not reading server responses up to the prompt, after each command you write.
PROMPT = ':~$'
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(PROMPT)
tn.write('ls\n')
print tn.read_until(PROMPT)
tn.write('exit\n')
btw, telnetnetlib can be tricky and things varies depending on your FTP server and environment setup. you might be better off looking into something like pexpect to automate login and user interaction over telnet.
I struggled for a while trying to write to a SynAccess power strip. This is how I did it:
import sys
import telnetlib
HOST = < your SynAccess switch ip address >
user = < user name >
password = < password >
tn = telnetlib.Telnet(HOST, 23, 5)
tn.write("login\r\n")
tn.write(user + "\r\n")
tn.write(password + "\r\n")
tn.write("rb 3\r\n") # this reboots plug 3
tn.write("rb 1\r\n") # this reboots plug 1
tn.write("logout\r\n")
tn.close
use python 2.7 or use a higher version with"(" ,")" at last line
If none of these answers worked, you can also try:
tn.read_until(b"Password: ")
tn.write(password.encode('utf-8') + b"\n")
tn.read_until(b"Terminal type?", 5)
tn.write("vt100\n".encode('utf-8'))
tn.read_until(b">", 5)
Even though I never saw the actual request for the terminal type being printed, this write seemed to work.