带有Yes No option

时间:2018-09-02 00:24:59

标签: python python-3.x

我对Python3(和Python 2.x)非常陌生 我经常使用nmap定期扫描服务器,以确保打开或关闭正确的端口。

我的目标是编写一个带有nmap的Python3脚本,供其他IT人员使用。我希望脚本执行以下操作:

  1. 选择一个扫描选项

    option 1 quick scan
    option 2 most common tcp ports
    option 3 scan ports 1-6000
    
  2. 向用户询问是否要将扫描结果写入输出文件,或者只是运行扫描并从终端读取输出。

  3. 输入要扫描的IP地址

我能够为1.和3.编写代码,但是我似乎无法编写带有选项的代码,是的,我是否想要输出文件

os.system("nmap -T4 -A -v -Pn -oN outputfile.txt"+ str(ip)

或者不,我不需要输出文件

os.system("nmap -T4 -A -v -Pn "+ str(ip)

我希望我的帖子很清楚。我很乐意分享我已经编写的代码。

这是代码。我肯定有错误。任何帮助将不胜感激。谢谢。

#!/usr/bin/python

#Library
import os, sys, time
print (sys.argv)

import subprocess

# Clear the screen
subprocess.call('clear', shell=True)

print('Welcome to ScanNmap')
print(' ')

def main():

    print('Please make your selection')
    print(' ')
    print('[1] Quick scan')
    print('[2] most common tcp ports + OS detection')
    print('[3] Scan - all TCP ports.')

    print('[9] Exit.')
    print(' ')

    option = input('Choose your Scanning Option:')


    if (option == 1):
        print('Do you want an output file?')
        answer = input()
        if answer == 'no':      
            ip = input('Input IP Address / Hostname:')
            os.system("nmap -T4 -v -Pn"+ str(ip))
            print('\n[**] Done \n')
            main()

    else answer == 'yes':
        ip = input('Input IP Address / Hostname:')
        os.system('nmap -T4  -v -Pn -oN outputfile.txt'+ str(ip)
        #print("\n[**] Done \n")
        main()  

    if (option == 2):
        print('Do you want an output file?')
        answer = input()
        if answer == 'no':      
            ip = input('Input IP Address / Hostname:')
            os.system('nmap -T4 -A -v -Pn'+ str(ip))
            print('\n[**] Done \n')
            main()

    else answer == 'yes':
        ip = input('Input IP Address / Hostname:')
        os.system('nmap -T4 -A -v -Pn -oN outputfile.txt'+ str(ip)
        print('\n[**] Done \n')
        main()

    if (option == 3):
        print('Do you want an output file?')
        answer = input()
        if answer == 'no':      
            ip = input('Input IP Address / Hostname:')
            os.system('nmap -T4 -p- -v -Pn'+ str(ip))
            print('\n[**] Done \n')
            main()

    else answer == 'yes':
        ip = input('Input IP Address / Hostname:')
        os.system('nmap -T4 -p- -v -Pn -oN outputfile.txt'+ str(ip)
        print('\n[**] Done \n')
        main()



    else:
    print("\nInvalid Option..Let's try again >>\n")
        main()


if __name__ == "__main__":
    try:
        main()

    except KeyboardInterrupt: 
        print("\n Keyboard  has been stopped :(")
        print("\n[**] Stopping nmap scan.. Thank you for using NmapScan \n")
        time.sleep(2)
        pass

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/python

#Library
import os, sys, time
print (sys.argv)

import subprocess

# Clear the screen
subprocess.call('clear', shell=True)

print('Welcome to ScanNmap')
print(' ')

def main():

    print('Please make your selection\n')
    print('[1] Quick scan')
    print('[2] most common tcp ports + OS detection')
    print('[3] Scan - all TCP ports.')

    print('[9] Exit.')
    print('\n')

    option = int(input('Choose your Scanning Option:'))

    print(option,type(option))
    if (option == 1):
        print('Do you want an output file?')
        answer = input()
        if answer == 'no':      
            ip = input('Input IP Address / Hostname:')
            os.system("nmap -T4 -v -Pn"+ str(ip))
            print('\n[**] Done \n')
            main()

        elif answer == 'yes':
            ip = input('Input IP Address / Hostname:')
            os.system('nmap -T4  -v -Pn -oN outputfile.txt'+ str(ip))
            print("\n[**] Done \n")
            main()  

    if (option == 2):
        print('Do you want an output file?')
        answer = input()
        if answer == 'no':      
            ip = input('Input IP Address / Hostname:')
            os.system('nmap -T4 -A -v -Pn'+ str(ip))
            print('\n[**] Done \n')
            main()

        elif answer == 'yes':
            ip = input('Input IP Address / Hostname:')
            os.system('nmap -T4 -A -v -Pn -oN outputfile.txt'+ str(ip))
            print('\n[**] Done \n')
            main()

   if (option == 3):
       print('Do you want an output file?')
       answer = input()
       if answer == 'no':      
           ip = input('Input IP Address / Hostname:')
           os.system('nmap -T4 -p- -v -Pn'+ str(ip))
           print('\n[**] Done \n')
           main()

    elif answer == 'yes':
        ip = input('Input IP Address / Hostname:')
        os.system('nmap -T4 -p- -v -Pn -oN outputfile.txt'+ str(ip))
        print('\n[**] Done \n')
        #main()

else:
    print("\nInvalid Option..Let's try again >>\n")
    #main()


if __name__ == "__main__":

    try:
       main()

    except KeyboardInterrupt: 
        print("\n Keyboard  has been stopped :(")
        print("\n[**] Stopping nmap scan.. Thank you for using NmapScan \n")
        time.sleep(2)
        sys.exit(0)

我已经更正了您的代码,但我认为您不清楚您的问题,如果您说要如何扫描IP(通过文件输入/动态扫描)会更好。另一个想法是,您可以使用nmap库以非常有效的方式完成它,而只需更少的代码行数。

链接供您参考。

python-nmap basic tcp scanner