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

2016-05-25 Thread Alan Gauld via Tutor
On 25/05/16 17:19, Alan Gauld via Tutor wrote:

> Here is an actual session using a public telnet site:
> 
 import telnetlib
 tn = telnetlib.Telnet('telehack.com')
 response = tn.read_some()
 b'\r\nConnected to TELEH'

Oops! a cut n paste error. That line should be:

>>> print(response[:20])
b'\r\nConnected to TELEH'


 tn.close()


-- 
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2016-05-25 Thread Alan Gauld via Tutor
On 25/05/16 14:11, Angelia Spencer wrote:
> in your code below you're telnet-ing to a website,

No, I'm telnetting to a server with the IP address mysite.com
(which is obviously fictitious, but could be any valid IP address).
There is nothing that says it's a web site. (And even some web
sites might allow telnet access, that's just an admin thing)

> I am not and when I type, >>> response = tn.read(). I get an error.
>
> >>> response=tn.read()
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: Telnet instance has no attribute 'read'
> >>>

Sorry, I misremembered the method name.
Here is an actual session using a public telnet site:

>>> import telnetlib
>>> tn = telnetlib.Telnet('telehack.com')
>>> response = tn.read_some()
>>>b'\r\nConnected to TELEH'
b'\r\nConnected to TELEH'
>>> tn.close()
>>>


There are a bunch of other read_() methods, you
need to read the help page to find out how they differ.
-- 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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2016-05-25 Thread Alan Gauld via Tutor

> I do get the >>> in the python IDLE but within my python script/file can
> I telnet to my controller? Keep in mind when I do log into my controller
> it's command line driven.

One thing that occurred to me is that you may be better off using the
subprocess module to start an interactive telnet process. It's less easy
to control the session programmatically than with telnetlib
but it would give you the interactive element you seem to want.

It just depends on what you are trying to achieve...

-- 
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2016-05-24 Thread Alan Gauld via Tutor
Forwarding to the list.
Please use reply-all to respond to list messages.

Also please use plain text as HTML messages often result in
code listings being corrupted, especially the spacing, which
is very important in Python.


 Forwarded Message 

> I just opened the python IDLE 3.5 on my MAC and I imported telnet.lib.
> On the next line I typed telnet 192.09.168.55 and I got an error. It said
> invalid syntax. I'm trying to telnet to my other MAC here at home, just
> to see if I can connect.

You cannot just type telnet commands into Python you need to use
the telnet API. (Type help(telnetlib) at the >>> prompt or visit the
modules documentation page)

A typical session might look something like:

>>> import telnetlib
>>> tn = telnetlib.Telnet('myhost.com')
>>> response = tn.read()
>>> print(response)
. some stuff here 
>>> tn.close()

That's assuming you have telnet access to myhost.com of course, many sites
don't allow it because of the security issues associated with telnet.
ssh is
probably a better bet.

But in either case don't expect a telnet interactive session - that's
what the
telnet command (or ssh) is for. Python gives you the ability to automate a
session, with no human interactivity required. If you want to interact
you'll
need to read the output and check for prompts from the host then relay
those prompts to your user from Python.
 

-- 
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2016-05-24 Thread Alan Gauld via Tutor
Forwarding to list...



 Forwarded Message 

The box is my controller with and IP address, I'm doing all this from my
windows 7 PC. 
As I said I can type telnet 10.35.56.90 in the dos cmd prompt and get to
my controller. I wrote a python script with the user_acct dictionary.

I do get the >>> in the python IDLE but within my python script/file can
I telnet to my controller? Keep in mind when I do log into my controller
it's command line driven.

I have python 2.7 and 3.5 installed on my windows 7 pc.

So you're saying open the python IDLE and import the telnet lib and just
type; telnet 10.45.34.80 and I'll be able to get to my controller???

Thank you for helping me :)
 
...you cannot direct the wind but you can adjust your sails...
 
 
*Angelia Spencer (Angie)*



*From:* Alan Gauld via Tutor 
*To:* tutor@python.org
*Sent:* Tuesday, May 24, 2016 2:38 PM
*Subject:* Re: [Tutor] I've subscribed to your service, no confirmation
yet. I'm looking for a tutor and I need help with some code.

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