Hello, I am new to python programming, and was attempting to get my feet wet by trying the popular telnet program on the web; here is the program. The problem is; I am trying to telnet to a switch, and execute some programs, but I am getting no where:

Please help me get started; once I get this first step going, I believe I could build on it.

Thanks.

**import getpass
import sys
import telnetlib
HOST = '192.168.10.2'
user = raw_input('Enter your remote account: ')
password =import getpass
import sys
import telnetlib
HOST = '192.168.10.2'`Inline Code Example Here`
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('vt100\n')
tn.write('ls\n')
tn.write('exit\r\n')

print tn.read_all**

You have syntax errors at lines 1, 6, 9 and 22.

Thanks pyTony; those errors are not in my code; it was my first time posting, so some weird stuff happened when i was pasting my code.

Anyway, on Line 1 and 22, the double stars (**) were added when I applied BOLD to the code. Line 6 shouldn't have that "Inline....." in there; that part is not in my code. And, line 9 does have a space on both sides of the "=" sign.

Here it is again:

import getpass
import sys
import telnetlib
HOST = '192.168.10.2'
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('vt100\n')
tn.write('ls\n')
tn.write('exit\r\n')

print tn.read_all()

Thanks again.

I have a small python script to telnet into a device and issue a reboot.

This works fine for me:

#!/usr/bin/python

import getpass
import sys
import telnetlib

HOST = "host ip goes here"
user = "user goes here"
password = "password goes here"

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")

tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("~ $")
tn.write("reboot\n")
tn.write("exit\n")

tn.read_all()

ajike: Are you certain that the box you are trying to connect to has a telnet server running, and that port 23 isn't being blocked by the firewall on either system? Most system installations disable telnet by default these days, because of security concerns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.