Python Telnetlib connect the port other than 23

0

Hello;

I'm trying to make a python script to connect to port 12612 of localhost. Running the telnet command in linux, manually, it executes. However, in my python script, using the telnetlib library, it apparently is not connecting. I need to connect to the telnet console, and perform some tasks. Here is 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')
    
asked by anonymous 03.11.2018 / 21:17

1 answer

0

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

If you created the server like this:

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

open has already been called for you. Calling tn.open() later will invalidate the connection, as it says in the documentation:

  

Do not reopen an already connected instance.

    
06.11.2018 / 18:01