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.
Related
I'm trying to write a script to connect to VPN more easily. I want the script to use the user input and connect to a vpn endpoint via openvpn, print the process ID for the user, and then continue running in the background but return the user to the shell. I tried child.close(force=False), os.fork(), and a few other things without any luck. The code I'm using is below. Any advice is much appreciated!
import getpass
import os
import pexpect
import random
import subprocess
import sys
country = sys.argv[1] if len(sys.argv) > 1 else 'US'
print('Connecting to VPN through ' + country)
vpns=subprocess.run(['ls', '/etc/openvpn/'], capture_output=True).stdout.decode().split('\n')
matching_vpns=filter(lambda vpn: vpn[:2].lower() == country.lower(), vpns)
chosen_vpn = random.choice(list(matching_vpns))
print('Connecting to ' + chosen_vpn + '\n')
child = pexpect.spawnu('sudo openvpn /etc/openvpn/'+chosen_vpn)
child.expect('Enter Auth Username:')
username = input('Enter Auth Username: ')
child.sendline(username)
child.expect('Enter Auth Password:')
password = getpass.getpass('Enter Auth Password: ')
child.sendline(password)
child.logfile_read = sys.stdout
while True:
try:
child.expect('\n', timeout=None)
if 'Initialization Sequence Completed' in child.before:
print("\nSuccessfully connected to " + chosen_vpn)
print("To stop VPN, run 'sudo pkill -9 -P " + str(child.pid) + "'")
break
except pexpect.EOF:
break
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.
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.
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()