0

I'm working on a Telnet client. I started coding on my notebook(Windows) and at the finish I uploaded it on my server(Debian). Both systems works with Python 3. At my notebook the script works well, but on Debian, it does make errors.

The Code:

import telnetlib
import sys
try:
    HOST = sys.argv[1]
    user = sys.argv[3]
    password = sys.argv[4]
    cmd= sys.argv[5]
    port=int(sys.argv[2])

    tn = telnetlib.Telnet(HOST, port)

    tn.read_until(b"username: ")
    tn.write(user.encode('ascii') + b"\n")
    if password:
        tn.read_until(b"password: ")
        tn.write(password.encode('ascii') + b"\n")

    tn.write(cmd.encode('ascii') + b"\n")
except ConnectionRefusedError:
    print('ERROR')
else:
    print('OK')

Server(CraftBukkit server with RemoteToolKit):

Mar 05, 2014 12:39:58 PM net.wimpi.telnetd.net.ConnectionManager makeConnection
INFO: connection #1 made.
Unexpected error in shell!
java.net.SocketException: Connection reset
>       at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118)
>       at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
>       at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
>       at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
>       at java.io.DataOutputStream.flush(DataOutputStream.java:123)
>       at net.wimpi.telnetd.io.TelnetIO.flush(Unknown Source)
>       at net.wimpi.telnetd.io.TerminalIO.flush(Unknown Source)
>       at net.wimpi.telnetd.io.TerminalIO.write(Unknown Source)
>       at com.drdanick.McRKit.Telnet.ConsoleShell.run(ConsoleShell.java:78)
>       at net.wimpi.telnetd.net.Connection.run(Unknown Source)
Mar 05, 2014 12:39:58 PM net.wimpi.telnetd.net.ConnectionManager cleanupClosed
INFO: cleanupClosed():: Removing closed connection Thread[Connection1,5,]

Greets miny

EDIT: The error handling works now! THX @ Wojciech Walczak The client doesn't report errors, but the server reports errors. If I run the same code on Windows, it doesn't make errors.

miny1997
  • 469
  • 3
  • 13

5 Answers5

1

...and are you sure you're using Python 3.3 or later? ConnectionRefusedError has been added in Python 3.3.

EDIT:

Given that your client works fine when launched from your laptop, and is catching ConnectionRefusedError on another machine, I would say that the problem is not the script itself. It's rather about server's telnet/firewall settings. Are other telnet clients working in the environment in which your script is failing?

Wojciech Walczak
  • 3,419
  • 2
  • 23
  • 24
0

The reason for the traceback when the server is offline is because you are trying to trap a non-existent exception (which is to say that the name ConnectionRefusedError has not yet been assigned a value).

Solely for its educational purpose I would remove the `try ... except ..." and let the error be raised Then hopefully you will find out exactly what exception is being raised.

As to the Java traceback, WTF?

holdenweb
  • 33,305
  • 7
  • 57
  • 77
0

The error is in the script. The telnet command on Linux works well.

miny1997
  • 469
  • 3
  • 13
0

I ran the code w/o try and except and it doesn't report errors.

I made some tests with byte strings.

If I run this code, the machines displays different strings.

Command:

print(b"Hello there!")

Windows:

b"Hello there!"

Linux:

Hello there!
miny1997
  • 469
  • 3
  • 13
  • I presume that there's still an error on the server side? If so, what happens when you drop the `b`'s in your telnet client? (Please, answer below my comment.) – Wojciech Walczak Mar 05 '14 at 19:56
  • There's no error at server-side. I tested the server with PuTTY. `tn.write(b'say Hi')` That works `tn.write("say Hi".encode('ascii'))` That doesn't work – miny1997 Mar 05 '14 at 20:02
  • Is there any problem, then? – Wojciech Walczak Mar 05 '14 at 20:02
  • Traceback (most recent call last): File "UDPClient.py", line 12, in tn.read_until("username: ") File "C:\Python33\lib\telnetlib.py", line 299, in read_until return self._read_until_with_select(match, timeout) File "C:\Python33\lib\telnetlib.py", line 352, in _read_until_with_select i = self.cookedq.find(match) TypeError: Type str doesn't support the buffer API – miny1997 Mar 05 '14 at 20:31
  • The traceback suggests that the problem is in `tn.read_until()` function, and not in the `tn.write()` function. Could you update your original message with your current code and current errors? In general, the error you're seeing now is a matter of mixing bytes and strings. Try: `b'str1, str2'.find('str')` and `b'str1, str2'.find(b'str')`. – Wojciech Walczak Mar 05 '14 at 20:48
0

Updated Code at Debian (Windows uses still the old code):

import telnetlib
import sys
if(True):
    HOST = sys.argv[1]
    user = sys.argv[3]
    password = sys.argv[4]
    cmd= sys.argv[5]
    port=int(sys.argv[2])

    tn = telnetlib.Telnet(HOST, port)

    tn.read_until(b"username:")
    tn.write(user.encode('ascii') + b"\n")
    if password:
        tn.read_until(b"password:")
        tn.write(password.encode('ascii') + b"\n")

    tn.write(cmd.encode('ascii') + b"\n")

I tested the code at Windows and the telnet server can run the commands. When I run the code at Debian, it doesn't say nothing, but the server says *Connection reset".

miny1997
  • 469
  • 3
  • 13