[geeks] Using Python3 to Telnet to OpenVMS

Lex Landa brooknet at imap.cc
Wed May 29 19:29:08 CDT 2013


Hello Mark,

I was a bit late in reading your message; I hope that this is helpful
anyway.

On Fri, 12 Apr 2013, at 11:08, Mark Benson wrote:

> I am trying to build an automation interface to use telnet to connect,
> run a command, return the terminal output, then close the connection.

> Which should, by rights, do something. Thus far it doesn't work, it never
> even detects the 'Username: ' string. I don't know enough Python to poke
> it really hard although I'm RTFMing it as I go.

The same applies to me - I know very little Python, and am consulting
the documentation for everything.

> I am guessing, based on the fact the skeleton of this script is lifted from the
> Python 3.2 docs (thus probably works on generic unix-like systems), that perhaps
> OpenVMS is introducing something hairy?

It does indeed seem to be doing something strange - outputting great
strings of BS chrs, for one - although there are problems with the
Telnet library read_until() function on my system, and it only accepts a
string if it has a newline after it.

Another issue was that "\r" doesn't send a CR (I'm not sure if it sends
anything at all) so I replaced that with "\015".

> I am connecting via a simh instance with a simulated DZV11 connection. Is this possible issue?

I am using the same configuration.

I edited your script a bit, added some debug statements and eventually
arrived at this (please break at the 'snip' and 'unsnip' lines):

--- snip ---

#!/usr/bin/python3

#required libs
import getpass
import telnetlib

#connection spec
HOST = "vax"
port = 23

#make connection
tn = telnetlib.Telnet(HOST,port)

# Enable debugging
tn.set_debuglevel(1)

#DZV11 requires a CR to start login process, so send one
print("Sending CR")
tn.write(b"\n")

#wait for the Username: prompt
print("Waiting for username")

# A problem: read_until waits for the string + CR.. verify this?
tn.read_until(b"Username: ")

#get a username from the user
user = input("Enter your remote account: ")

# Send the username, followed by a CR
# No CR is sent if you use '\n', so I replaced it with '\015' (decimal
13).
# tn.write(user.encode('ascii') + b"\n")
tn.write(user.encode('ascii') + b"\015")

#request a password
print("Please enter password.")

#Use getpass to capture the password without echo
password = getpass.getpass()

#wait for the Password: prompt and then enter it followed by CR
print("Waiting for password prompt")
tn.read_until(b"Password:")
print("Got it. Sending password")
tn.write(password.encode('ascii') + b"\015")

#wait for a $ prompt
print("Waiting for shell prompt")
tn.read_until(b"$")
#DIR command
tn.write(b"DIR\015")

#wait for another $ prompt
tn.read_until(b"$")
#DIR command
tn.write(b"DIR\015")

#wait for another $ prompt
tn.read_until(b"$")
#Log out
tn.write(b"LO\015")

#Print whatever comes back until EOF
print(tn.read_all().decode('ascii'))

tn.close()
tn.set_debuglevel(0)

--- unsnip ---

This works for me, although there are some odd pauses - could be that
OpenVMS doesn't like getting text at a certain rate, or its output is
being buffered by read_all() and it's then flushing the buffer and
returning the output to print() (in the 'Print whatever comes back until
EOF' bit).

The script's output is a bit messy, since I/O is all mixed-up with the
debug output.  It should look a bit like this:

--- snip ---

lex at circadian:~$ ./telnet.py
Sending CR
Telnet(vax,23): send b'\n'
Waiting for username
Telnet(vax,23): recv b'\xff\xfb\x01\xff\xfb\x03'
Telnet(vax,23): IAC WILL 1
Telnet(vax,23): IAC WILL 3
Telnet(vax,23): recv b'\xff\xfc\x01'
Telnet(vax,23): IAC WONT 1
Telnet(vax,23): recv b'\xff\xfb\x03'
Telnet(vax,23): IAC WILL 3
Telnet(vax,23): recv b'\r\n\r\n Welcome to OpenVMS (TM) VAX Operating
System,'
Telnet(vax,23): recv b' Version V7.3    \r\n\r\n\rUsername: '
Enter your remote account: LEX
Telnet(vax,23): send b'LEX\r'
Please enter password.
Password: 
Waiting for password prompt
Telnet(vax,23): recv b'LEX\r\n\rPassword: '
Got it. Sending password
Telnet(vax,23): send b'PASSWORD\r'
Waiting for shell prompt
Telnet(vax,23): recv b'\r\n Welcome to OpenVMS (TM) VAX Operating
System, V'
Telnet(vax,23): recv b'ersion V7.3\r'
Telnet(vax,23): recv b'\n    Last interactive login on Wednesday,
29-MAY-2'
Telnet(vax,23): recv b'013 23:43\r\n    Last non-interactive login on
Satur'
Telnet(vax,23): recv b'day, 22-SEP-2012 00:26\r'
Telnet(vax,23): recv b'\x1b[c'
Telnet(vax,23): recv b'\x1b[c'
Telnet(vax,23): recv b'\x1b\\\x1bZ'
Telnet(vax,23): recv b'\x1b[0c'
Telnet(vax,23): recv b'\r\n%SET-W-NOTSET, error modifying TNA37:\r'
Telnet(vax,23): recv b'\n-SET-I-UNKTERM, unknown terminal type\r'
Telnet(vax,23): recv b'\n\x00$ '
Telnet(vax,23): send b'DIR\r'
Telnet(vax,23): recv b'\r\n\r'
Telnet(vax,23): recv b'\nDirectory
DUA1:[LEX]\r\n\r\nASTEROIDS.$PACKAGE;2\r\n   '
Telnet(vax,23): send b'DIR\r'

[More DIR output follows - I have deleted it because there is a lot of
output.]

Total of 85 files, 970 blocks.
$ 
  LEX          logged out at 30-MAY-2013 00:12:24.02

--- unsnip ---

Because OpenVMS is echoing everything, it would be useful to disable
this, which can possibly be done with calls to the telnet library, or by
putting a SET TERMINAL /NOECHO command in the command list to the VAX.

Lex


More information about the geeks mailing list