0

I have a small script that will check to see if a list of devices are either ssh or telnet enable. Here is my code:

import socket
import sys
file = open('list', 'r')
file = file.readlines()
list = []
for i in file:
 i=i.replace('\n','')
 list.append(i)
for i in list:
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 try:
 s.connect((i, 22))
 s.shutdown(2)
 s.close()
 print (i+' SSH ')
 except:
 try:
 s.connect((i, 23))
 s.shutdown(2)
 s.close()
 print (i+' Telnet')
 except:
 print (i + 'disable')
 pass

When I get an exception, I have to hit ctrl + c to go to the next device. What are am I doing wrong? Thanks

dansalmo
11.7k5 gold badges62 silver badges55 bronze badges
asked Jun 13, 2013 at 17:09
4
  • 1
    You should not catch all exceptions. Catch specific exceptions only instead. Commented Jun 13, 2013 at 17:12
  • 3
    Try except Exception as e and then figure out the type of e by printing type(e). Also try printing before each statement in your try block so you know where you're getting stuck. Commented Jun 13, 2013 at 17:14
  • 1
    You should not use list as a name since it a built in name. Commented Jun 13, 2013 at 17:24
  • 1
    One word: nmap Commented Jun 13, 2013 at 17:24

3 Answers 3

1

did you try adding a timeout?

import socket
import sys
with open('list', 'r') as f:# file is a global class
 # per default it reads the file line by line, 
 # readlines() loads the whole file in memory at once, using more memory
 # and you don't need the list.
 for i in f:
 i=i.replace('\n','')
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.settimeout(10)
 try:
 s.connect((i, 22))
 s.shutdown(2)
 s.close()
 print (i+' SSH ')
 except:
 try:
 s.connect((i, 23))
 s.shutdown(2)
 s.close()
 print (i+' Telnet')
 except:
 print (i + 'disable')
 pass

setting up a time out closes the stream after a timeout, otherwise it blocks forever.

answered Jun 13, 2013 at 17:39
Sign up to request clarification or add additional context in comments.

Comments

1

I cannot really run the code because I don't have the list file you open on my machine. Still made few edits, any difference?

import socket
import sys
file = open('list', 'r')
file = file.readlines()
list = []
for i in file:
 i=i.replace('\n','')
 list.append(i)
for i in list:
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 try:
 s.connect((i, 22))
 s.shutdown(2)
 s.close()
 print (i+' SSH ')
 except:
 s.connect((i, 23))
 s.shutdown(2)
 s.close()
 print (i+' Telnet')
 else:
 print (i + 'disable')
 pass
answered Jun 13, 2013 at 17:17

Comments

0

Again I can't really run the code because of lack of the file 'list' (rather misleading..) but I've done some further refactoring and offered up a suggestion.

import socket
import sys
with open('list', 'r') as f:
 # Don't call anything 'list' as it is the name for pythons inbuilt type
 # Using `with` will automatically close the file after you've used it.
 content = f.readlines()
 # We can use a list comprehension to quickly strip any newline characters.
 content = [x.replace('\n','') for x in content]
for x in content:
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 try:
 s.connect((x, 22))
 s.shutdown(2)
 s.close()
 print (x+' SSH')
 except:
 # This should catch a specific exception, e.g. TimeoutError etc
 # e.g. `except ConnectionError`
 try:
 s.connect((i, 23))
 s.shutdown(2)
 s.close()
 print (i+' Telnet')
 except:
 print (i + 'disable')
 pass

My guess is that the connection is hanging rather than hitting the exception. Maybe due to timeouts as it cannot connect.

Add a timeout option with:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(60)
answered Jun 13, 2013 at 17:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.