Python 3 - 我的端口扫描程序的输出正在打印单词None

时间:2018-05-24 14:59:58

标签: python

当我运行我的端口扫描程序脚本时,它工作正常,但它也打印单词“无”。我只是想检查我从csv文件上传的设备上是否打开了端口22 如何删除这个无字?

10.38.0.45 - 端口22:好的

10.38.0.46 - 端口22:好的

10.38.0.47 - 端口22:好的

#!/usr/bin/env python
import socket
import sys
from datetime import datetime


def check(ip, port):

    ip_address = ip
    port = port
    portlist1 = [22]
    portlist2 = [23]
    # Clear the screen
    # subprocess.call('clear', shell=True)

    # Ask for input
    # remoteServer    = raw_input("Enter a remote host to scan: ")

    remote_server_ip = socket.gethostbyname(ip_address)

    # Print a nice banner with information on which host we are about to scan
    # print "-" * 60
    # print "Please wait, trying to reach remote host(s)"#, remoteServerIP
    # print "-" * 60

    # Check what time the scan started
    t1 = datetime.now()

    # Using the range function to specify ports (here it will scans all ports between 1 and 1024)

    # We also put in some error handling for catching errors
    if port == '':
        print('Please set only ports 22 or 23 first in excel file to check connectivity')
        sys.exit()
    if port == '22':
        try:
            for port in portlist1:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(3)
                result = sock.connect_ex((remote_server_ip, port))

                if result == 0:
                    print('\n'+remote_server_ip+" - Port {}:     OK\n".format(port))
                else:
                    print('\n'+remote_server_ip+" - Not able to connect ok Port {}: \n".format(port))
                sock.close()

        except KeyboardInterrupt:
            print("You pressed Ctrl+C")
            sys.exit()
        except socket.error:
            print(ip+": Couldn't connect to device")
            # sys.exit()
        except 10060:
            print("The host was not reachable")
        except socket.gaierror:
            print('Check IP Address')
        except 11004:
            print("The host was not reachable")
        except OSError as e:
            print(e, "import(%d): Library not found" % sys.exc_info()[-1].tb_lineno)

    else:
        try:
            for port in portlist2:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(3)

                result = sock.connect_ex((remote_server_ip, port))
                sock.close()
                if result == 0:
                    if result == 0:
                        print(remote_server_ip + " - Port {}:    OK\n".format(port))
                else:
                    print(remote_server_ip + " - Not able to connect on Port {}: \n".format(port))

        except KeyboardInterrupt:
            print("You pressed Ctrl+C")
            sys.exit()
        except socket.error:
            print(ip + ": Couldn't connect to server")
            # sys.exit()
        except 10060:
            print("The host was not reachable")
        except socket.gaierror:
            print('Check IP Address')
        except 11004:
            print("The host was not reachable")
        except OSError as e:
            print(e, "import(%d): Library not found" % sys.exc_info()[-1].tb_lineno)
    # Checking the time again
    t2 = datetime.now()

    # Calculates the difference of time, to see how long it took to run the script
    total = t2 - t1

    # Printing the information to screen
    # print 'Scanning Completed in: ', total

1 个答案:

答案 0 :(得分:1)

您的功能并非return任何内容,因此默认情况下会返回None。如果在print语句中调用该函数,则也会打印None

例如:

def test_func(num):
    if num == 1:
        print("This is a one!")
    else:
        print("This is not a one :(")

print(test_func(1))
# This is a one!
# None