Python Telnetlib connect port other than 23

Asked

Viewed 383 times

1

Hello;

I’m trying to make a python script to connect to port 12612 localhost. Running the telnet command on linux, manually, it runs. However, in my python script, using the telnetlib library, it is apparently not connecting. I need to connect to the telnet console, and perform some tasks. Follow the code:

import os, glob, telnetlib, time

CAMINHO = "/home/admin/deploy"
HOST = "localhost"
PORT = "12612"
TIMEOUT = 2
filename = os.path.basename(__file__)
os.chdir(CAMINHO)

try:
    tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
    tn.set_debuglevel('DEBUG')
    tn.open(HOST, PORT, TIMEOUT)
except ValueError:
    print("Falha ao conectar")

for file in glob.glob("*.jar"):
    arquivoAtual = file.split('_')[0]
    comando = ("file://" + CAMINHO + "/" + file).strip()
    tn.write(("uninstall " + arquivoAtual + "\n"))
    tn.write("install " + comando + "\n")
    tn.write("setbsl 5" + arquivoAtual + "\n")
    tn.write("start " + arquivoAtual + "\n")


tn.write("exit" + "\n")
tn.close()
print('Concluido')
  • "apparently not connecting" as so, what happens? you get some kind of error?

  • No error, but also, commands are not executed.

  • Do you get the "Failed to connect" message? And the "Done" message at the end?

  • I placed after the for a Tn.read_all(), and now gives the following error: python deploy.py Traceback (Most recent call last): File "deploy.py", line 32, in <module> Tn.read_all() File "/usr/lib/python2.7/telnetlib.py", line 385, in read_all self.fill_rawq() File "/usr/lib/python2.7/telnetlib.py", line 576, in fill_rawq buf = self.sock.recv(50) socket.timeout: timed out The completed message appears if I take out Tn.read_all().

  • Maybe he’s not getting into for - are sure you have files .jar in the current directory? Put a print(file) within the for to make sure that commands are being sent

  • Yes, the files are in the directory, I had already tested it ( with a print ). It’s quite strange this, I read in a place that, the library was made to work on port 23 with self negotiation. Any different port, which does not have auto-negotiation, may happen that the library cannot interact with the ftp server. ftp in question is idempiere’s osgi console. I’m making a script to update new application plugins with python.

  • Opa, Peraí, you said the server is FTP?? I thought it was telnet!! To access ftp servers, you must use the module ftplib and not telnetlib.

  • guy, sorry. It is telnet yes, only at door 12612.

Show 3 more comments

1 answer

0

There’s a problem with your code, which is the use of tn.open() after creating the instance with server and port name.

If you created the server like this:

tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)

The open has already been called to you. By calling tn.open() later you will be invalidating the connection, as it says in the documentation:

Do not reopen an already Connected instance.

  • I read in the documentation the following (https://docs.python.org/2/library/telnetlib.html) that the class instance is initially disconnected, use open() to connect. But I didn’t keep reading, where it says that if you pass the data to the constructor, it already makes the connection

  • Even without using open(), it does not connect. I set a timeout of 20 seconds, and put Tn.read_all() at the end, now gives socket.timeout().

  • Oops - this door there - it’s passing as string. I think it has to be number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.