Python Telnet with test driven development

311 views Asked by At

I'm writing some Python (3.4.2) scripts to telnet into our legacy VMS system to run some reports. The terminal emulation we use to access the system is VT320.

I want to use TDD into my scripts to help verify I'm at the right menu before proceeding.

Below is the start of my script. I'm not sure how to incorporate:
If test1 passes, do step1
If test2 passes, do step2
etc.

Has anyone incorporated TDD into a Python Telnet script to verify your location within the remote system?

Where do I go from here?

import unittest
import re
import telnetlib
timeout = 120
dataqueue = ""
f = open('datafile.txt', 'w')

class ConnectionTest(unittest.TestCase):
    def test_connection_to_Legacy_System(self):
        a = "---Test---"
        self.assertIsNone(a)
if __name__ == "__main__":
    unittest.main()

tn = telnetlib.Telnet(HOST,23,timeout)
tn.read_until(b"YOUR ID:")
tn.write(str.encode(user + "\r\n"))
tn.read_until(b"PASSWORD:")
tn.write(str.encode(password + "\r\n"))
tn.read_until(b"Selection: ")
tn.write("1\r\n")
dataqueue = tn.read_very_eager()
dataqueue = dataqueue.decode('ascii')

tn.close()
f.write(dataqueue)
f.close()
1

There are 1 answers

0
Hein On

I know nothing (about Python or TDD), but see some elements you may want to check.

\r has ASCII value 13 = CR = Carriage Return \n has ASCII value 10 = LF = LineFeed right?

When "Enter" is type only CR is send. The LF is the LF is part of the echo back to the terminal, not from the terminal.

Sending a LF is typically interpreted as "WORD DELETE" by the OpenVMS terminal driver. Now in the suggest usage that makes not difference because there is nothing to delete for the next input, but still.

SET HOST 0/LOG may well help show the characters needed to be send and expected back.

When I use SSH my prompts are "login as:" / "password:" and for telnet "Username: " / "Password: " I have not seen "YOUR ID:" / "PASSWORD:". Where is that supposed to come from? Is the session already logged and this a second level username/password?

Where do I go from here?

Where are you? What is the output so far? Succesfully Logged in? Where is "Selection: " supposed to come from? Menu from program? DCL Login.com ?

Good luck! Hein.