Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 3.6 telnet
#1
Anyone have a function script using 3.6 for telnet? The script below doesn't accomplish anything.RUnning a debug on the switch I see a telnet attempt but I don't see a login for the user and none of the commands run.

import getpass
import sys
import telnetlib

HOST = "X.X.X.X" # I use the actual IP here.
user = input("Enter your Username: ")
password = getpass.getpass()
print("passed the getpass step")

tn = telnetlib.Telnet(HOST)


tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
#tn.write(b"user" + "\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

# not needed due login privilege 15 to tn.write("enable\n")
tn.write(b"config t" + "\n".encode('ascii'))
print("config t complete")
tn.write(b"interface loopback 0" + "\n".encode('ascii'))
print("interface loopback 0")
tn.write(b"ip address 1.1.1.1 255.255.255.255" + "\n".encode('ascii'))
print("IP address complete")
tn.write(b"end" + "\n".encode('ascii'))
tn.write(b"exit" + "\n".encode('ascii'))

print(tn.read_all().decode('ascii'))
input("\n\nPress the enter key to exit.")
micseydel wrote Jan-29-2018, 09:30 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Quote
#2
In my case all commands are executed but the print(tn.read_all().decode('ascii')) function do not print anything.
Quote
#3
(May-04-2018, 10:27 PM)snagwekar Wrote: In my case all commands are executed but the print(tn.read_all().decode('ascii')) function do not print anything.

You can try this , it works for me, let me know if it did for you.

import getpass
import sys
import telnetlib

host = "10.0.0.10"
user = input("Enter Username:")
password = getpass.getpass()
tn = telnetlib.Telnet(host)
tn.read_until(b"Username:")
tn.write(user.encode("ascii")+ b"\n")

if password:
tn.read_until(b"Password:") (indent)
tn.write(password.encode("ascii")+b"\n") (indent)

tn.write(b"en \n")
tn.write(b"cisco\n")
tn.write(b"conf t\n")
tn.write(b"int loopback 1\n")
tn.write(b"ip add 1.1.1.1 255.255.255.255\n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode("ascii"))
Quote
#4
Hi Pallavi,

I tried using your code and I get the following error.

Traceback (most recent call last):

File "telnet.py", line 18, in <module>

tn.read_until(b"Password: ") (indent)

NameError: name 'indent' is not defined

How can I fix it.?



(Jul-25-2018, 03:09 PM)pallavi Wrote:
(May-04-2018, 10:27 PM)snagwekar Wrote: In my case all commands are executed but the print(tn.read_all().decode('ascii')) function do not print anything.

You can try this , it works for me, let me know if it did for you.

import getpass
import sys
import telnetlib

host = "10.0.0.10"
user = input("Enter Username:")
password = getpass.getpass()
tn = telnetlib.Telnet(host)
tn.read_until(b"Username:")
tn.write(user.encode("ascii")+ b"\n")

if password:
tn.read_until(b"Password:") (indent)
tn.write(password.encode("ascii")+b"\n") (indent)

tn.write(b"en \n")
tn.write(b"cisco\n")
tn.write(b"conf t\n")
tn.write(b"int loopback 1\n")
tn.write(b"ip add 1.1.1.1 255.255.255.255\n")
tn.write(b"end\n")
tn.write(b"exit\n")
print(tn.read_all().decode("ascii"))
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 1 132 Aug-10-2019, 06:44 PM
Last Post: DeaD_EyE
  Any suggestion on python library to use for both ssh and telnet? lord_mani 4 484 Jun-25-2019, 04:07 PM
Last Post: gb74razor
  telnet question jacklee26 2 409 Mar-30-2019, 06:45 AM
Last Post: jacklee26
  Retrieve output from telnet command Networker 1 403 Mar-12-2019, 01:36 PM
Last Post: searching1
  GNS3 telnet from bash terminal to virtual cisco router marienbad 2 475 Feb-24-2019, 07:55 AM
Last Post: marienbad
  Telnet to switch from jump start server(linux) searching1 0 455 Jan-09-2019, 10:03 PM
Last Post: searching1
  Issue when running telnet program using python3 searching1 11 2,102 Dec-22-2018, 12:48 AM
Last Post: searching1
  Aggregate multiple telnet connections Jibeji 1 1,538 Mar-02-2018, 07:21 PM
Last Post: mpd
  mysql connector/telnet issue (re: text game) rebubula76 1 676 Feb-06-2018, 08:00 PM
Last Post: rebubula76
  Multithread telnet not working Parallel anna 7 2,057 Feb-05-2018, 01:17 PM
Last Post: anna

Forum Jump:


Users browsing this thread: 1 Guest(s)