3

I have a remote server with an IP address and a port. I can connect to it using a standard command window successfully. When using the command window, I can see data flow through the window but I lack the ability to be notified if the data flow stops. I would like to use Python instead to accomplish the same task with the added feature of alerting me if/when data flow stops. I've attached the code that I've been working with below, is anyone able to help me with this process?

import sys
import telnetlib
import easygui

HOST = "###.###.###.###, 25"

tn = telnetlib.Telnet(HOST)

print tn.read_all()

tn.write("exit\n")

if data =="":
    easygui.msgbox("There's no data!!", title="Bummer Box")
Charles
  • 50,943
  • 13
  • 104
  • 142
Munkeyarms
  • 31
  • 2
  • I also forgot to mention that when I telnet in using a command window, I am not required to enter an ID or Password. The command looks like this: telnet ###.###.###.### 25 – Munkeyarms Dec 11 '12 at 20:40

1 Answers1

2

The first step is being able to connect to the host; you haven't mentioned if that's working yet. I believe tn.read_all will keep reading until there's an EOF. You might want tn.expect if you know what the data should look like or perhaps tn.read_lazy if you don't.

The next step is to be able to check the connection on a regular basis, say every minute or every ten seconds. The threading.Timer will help you schedule the task easily.

The third step is the notification. If the check for data is successful, just return silently. Only on the lack of data do you call to pop up a message box, send an email, or whatever.

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
TheSentinel
  • 111
  • 4
  • 1
    Yes, I can connect to the host in a command window, but not using Python. I get an error box that appears saying "invalid syntax" and I can only click "ok." Once I click okay, I'm brought back to my Python program and the "tn" is highlighted after the word print. – Munkeyarms Dec 11 '12 at 21:22
  • Do you get any info on where the error is being generated, such as the line number? Can you try running the script in idle to see where it conks out? – TheSentinel Dec 11 '12 at 23:02
  • I misread your comment, but I'm still not sure what's wrong. I changed the print line to read 'print tn.read_until("blah", 5)' and was able to connect to a nethack telnet server easily enough. – TheSentinel Dec 12 '12 at 18:11
  • Also, try `data = tn.read_until("blah", 5)` `print data` just in case checking on an uninitialized variable is somehow giving the syntax error. – TheSentinel Dec 12 '12 at 18:21