[Tutor] I've subscribed to your service, no confirmation yet. I'm looking for a tutor and I need help with some code.

Alan Gauld alan.gauld at yahoo.co.uk
Tue May 24 15:38:22 EDT 2016


Re your subject...
This is a mailing list. You subscribe and you should receive
mails sent to the list.
If you have a question send it to the list (like you did here)
and one or more of the list members will hopefully respond.
The more specific your question the more precise will be the response.
Try to include OS, Python version, any error messages(in full)

Now to your message...

On 24/05/16 18:06, Angelia Spencer via Tutor wrote:
>  I'm trying to telnet to my box. 

What is your "box"? A server somewhere? Running what OS?
Where are you telnetting from?

> When I do this in DOS it's simple, I even have a blinking cursor 
> for me to type in commands for each response.

I assume you mean you telnet from a Windows PC and login to your server
and get an OS command prompt? (Possibly running bash?)

> Not so for python. 

What does this mean? If you run the python command on your DOS
console you should get a prompt like

>>>

at which you can type in commands.

If python is installed on your "box" then telnet to the box
and at the OS prompt type python.

If that doesn't work for you, you will need to give us a lot more
information about how you installed Python, which version, how
you are trying to run python etc.

> I have a dictionary "user_acct" which has the username
> and password in it. 

> My box is interactive, I must be able to type in commands 

Again we have no idea what your "box" is. What do you mean
its interactive, nearly all computers are to some extent?

> and I don't know how to do this in python.

While python does have an interactive mode (the >>> prompt) it's not
normally used that way. Usually you put your code in a script file
(ending .py) and run it from an OS prompt (or file manager) like

C:\WINDOWS> python myscript.py

> 1st prompt = Username:2nd prompt = Password:
> 
> After this my box's output looks like this:Last Login Date      : May 24 2016 09:42:08
> Last Login Type      : IP Session(CLI)
> Login Failures       : 0 (Since Last Login)
>                      : 0 (Total for Account)
> TA5000>then I type en and get
> TA5000# then I type conf t and getTA5000(config)#

OK, I'm guessing that's a Unix like system but I'm not sure.

> My code is below:

How are you trying to run this?
Where is it stored?
Where is python installed?

> import getpass
> import sys
> import telnetlib

username = input()

> password = input()
> tid = 'TA5000'
> first_prompt = '>' # type 'en' at this prompt
> second_prompt = '#' # type 'conf t' at this prompt
> third_prompt = '(config)'
> prompt1 = tid + first_prompt
> prompt2 = tid + second_prompt
> prompt3 = tid + third_prompt + second_prompt
> user_acct = {'ADMIN':'PASSWORD','ADTRAN':'BOSCO','READONLY':'PASSWORD','READWRITE':'PASSWORD','TEST':'PASSWORD','guest':'PASSWORD','':'PASSWORD'}
> host = "10.51.18.88"
> #username = "ADMIN" + newline
> #password = "PASSWORD" + newline
> tn = telnetlib.Telnet(host,"23")
> open()

That calls the builtin open() function without arguments which should
cause an error. Do you get an error message?

You probably wanted

tn.open()

> tn.read_until("Username: ")
> tn.write(username)
> tn.read_until("Password: ")
> tn.write(password)


if username in user_acct and password == user_acct[username]:
>      print(prompt1 + "Enter en at this prompt" +"\n")
>      print(prompt2 + "Enter conf t at this prompt" + "\n")
>      print(prompt3 + "\n")
> else:
>      
>      print('Invalid Login... Please Try Again')close()

Shouldn't you check the login details before passing it
to the telnet host?

Also note you are not storing anything you get from the
host so you are just checking your own local data.

I don't really know what this is supposed to be doing.


I'd suggest starting slow. Create a script that simply
logs in with a hard coded name/password and then prints
a succcess/fail message and logs out again.

Once you know you can connect and login then you can start
to think about extra features.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list