1

I'm working on a telnet client for an IBM as400 box and have run into some trouble getting the right amount of data returned. Initially, the program should connect to a telnet session which has a username and password field which it writes to, then go to the user shell which lets employees input data, access a database, etc. When I call read_very_eager it returns the buffer filled with the login screen and the post-login session, instead of just the post-login session. I can't quite tell why this would occur, and other read_xyz methods also print nothing or have a buffer filled with just the first few bytes of the login screen. My code follows (sensitive data omitted):

def login():
    tn = telnetlib.Telnet(host, port)
    sleep(1)

    tn.write(username + "\t")
    tn.write(password + "\r\n")
    sleep(1)

    data = tn.read_very_eager()

    print data

def main():
    print "started"
    login()

main()
Bennet Leff
  • 376
  • 1
  • 4
  • 18
  • It just reads anything that's available. Why shouldn't it return the login prompt if nothing else has read it yet? – Barmar May 25 '18 at 15:02

1 Answers1

1

Read the login and password prompts before sending the answers. Otherwise they're kept in the stream and will be read by the next read call.

tn.read_some()
tn.write(username + "\t")
tn.read_some()
tn.write(password + "\r\n")
data = tn.read_some()

And use read_some() instead of read_very_eager() so you don't need to sleep first. It will wait for something to become available, then return everything that's there.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The first `read_some()` blocks, not quite sure why. If I comment it out, it runs but printing data shows the first line of my sign in screen instead of the signed in user screen. If I keep the first `read_some()` commented and replace the last `read_some()` call with a `sleep` and `read_very_eager()` it prints out everything like before. – Bennet Leff May 25 '18 at 15:25
  • I'm not sure why the first read blocks. Doesn't the server send a login prompt? – Barmar May 25 '18 at 15:26
  • It does send a login prompt. I suppose that's the true confusing part of the matter. Perhaps it rests on the fact that the IBM emulator green-screen (which the employees currently use) uses TN5250, which seems to be underdocumented/undersupported by libraries whereas my version uses plain telnet. Not quite sure beyond that what could be causing `read_some` to block, could it be something with windows? – Bennet Leff May 25 '18 at 15:47
  • 1
    Maybe you should use `read_until` with a timeout, to wait for each prompt. – Barmar May 25 '18 at 15:49
  • That definitely worked better. If I add `read_until` calls every time it reads too much and write what feels slightly "hard coded" by reading until an expected word of my choice, I can get it to write out essentially what I'm looking for. I wouldn't consider my solution perfect but I will definitely mark your response as the correct answer because, for all intents and purposes, you've answered my real question. – Bennet Leff May 25 '18 at 16:02