0

我有一个小脚本,可以检查设备列表是否启用了 ssh 或 telnet。这是我的代码:

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

当我遇到异常时,我必须按 ctrl + c 才能转到下一个设备。我在做什么错?谢谢

4

3 回答 3

1

我无法真正运行代码,因为我的list机器上没有您打开的文件。还是做了一些修改,有什么不同吗?

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
于 2013-06-13T17:17:05.237 回答
1

您是否尝试添加超时

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

设置超时会在超时后关闭流,否则它会永远阻塞。

于 2013-06-13T17:39:37.710 回答
0

同样,由于缺少文件“列表”(相当误导......),我无法真正运行代码,但我做了一些进一步的重构并提出了建议。

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

我的猜测是连接挂起而不是遇到异常。也许是由于超时,因为它无法连接。

添加超时选项:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(60)
于 2013-06-13T17:45:54.257 回答