0

I have a bunch of devices that I have to do an script to know if telnet is enable or not. So I did the following script:

import socket

file = open('list', 'r')
file = file.readlines()
list = []
port = 23 


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


for i in file:
    i=i.replace('\n','')
    list.append(i)
for i in host:
    try:
        s.connect((i, port))
        s.shutdown(2)
        print (i+' Telnet is enable') 
    except:
        print (i+' Telnet is disable') 

The problem is that after the first test that show telnet is enable, all of the devices are showing as been disable. I check the error that I get and the error is the following:

socket.error: (133, 'Transport endpoint is already connected')

I know that has to do with closing the first session, but I'm not able to. Thanks in advance

6
  • 1
    stackoverflow.com/a/13110002/477878 Commented Mar 4, 2013 at 14:39
  • Why not just close the connection? Commented Mar 4, 2013 at 14:41
  • if I do s.close() and then I try to open a new one, I get this error 'socket.error: (9, 'Bad file descriptor')' Commented Mar 4, 2013 at 14:45
  • 2
    @amb1s1 You'll need to close the old socket and create a new one, ie put the s = socket.socket(... inside the loop. Commented Mar 4, 2013 at 14:47
  • doing s.close() is not closing the old socket? Commented Mar 4, 2013 at 14:50

1 Answer 1

0

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

You'll need to close the old socket and create a new one, ie put the s = socket.socket(... inside the loop. – Joachim Isaksson

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.