Telnetlib does not properly close the socket

Asked

Viewed 46 times

0

I’m developing a python script that sends text using a telnet link to a portech mv-372.

The script works correctly but after some time the telnet connection drops and throws an exception "[Errno 32] Broken pipe". As the script is running as a systemd service whenever I encounter an error I finish the script and systemd restarts it.

When the script restarts it enters a loop because it cannot connect to the modem via telnet. The script only connects again when I reboot to the modem.

Here’s a copy of the code:

# Creare a Telnet Connection to Host1
try:
    tn = telnetlib.Telnet(HOST)
    sleep(1)
    tn.read_until('username: ')
    SendCommandToSocket(USERNAME, "password: ", 10, tn)
    SendCommandToSocket(PASSWORD, "info.\r\n]", 10, tn)
    print "Connection Established"
except Exception as e:
    print e
    logger.critical('Cannot connect to socket. Exception is: %s' %e)
    tn.close()
    sleep(10)
    exit()

Before tying the script I always close the telnet onexao using Tn.close().

Can anyone tell me why the connection is blocked after the script is finished?

1 answer

0

Can you do a packet analysis? Try using tcpdump or wireshark to do an analysis of the point that happens and then is being rejected. It seems that your Script is sending a package and it is receiving a reset. tcpdump -nvtttS iphost if you want to save add.... -Z $USER -w save.dump

Surely an analysis will give you a north to what’s happening.

Browser other questions tagged

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