【问题标题】:Python3 nmap script with Yes No option带有 Yes No 选项的 Python3 nmap 脚本
【发布时间】:2019-02-07 12:41:29
【问题描述】:

我是 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

【问题讨论】:

  • 通常分享你的代码是个好主意。
  • 我建议使用 python-nmap 库而不是使用 os.system。但是如果你只想使用系统命令,你可以使用 bash 将结果回显到一个文件中。

标签: python python-3.x


【解决方案1】:
#!/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

【讨论】:

  • 感谢您更正我的代码,非常感谢。我运行了代码,我得到了这些错误: 欢迎来到 ScanNmap 请做出您的选择 [1] 快速扫描 [2] 最常见的 tcp 端口 + 操作系统检测 [3] 扫描 - 所有 TCP 端口。 [9] 退出。选择您的扫描选项:1 1 您想要输出文件吗?没有 Traceback(最近一次调用最后一次):文件“nmapscan.py”,第 81 行,在 main() 文件“nmapscan.py”,第 32 行,在 main 中 ip = raw_input('Input IP Address / Hostname:' ) NameError: name 'raw_input' is not defined 我试图修复但没有运气
  • 如果你使用的是 python 3,你应该用 input() 替换它
  • 您还面临这个问题吗?我现在可以帮你。
  • - 感谢您的建议。我了解到 Python 2.3 xx 和 Python 3 完全不同!所以问题是固定的。我的下一个是,虽然我输入了要扫描的 IP,但我收到了这个错误消息。 “从以下位置读取数据文件:/usr/bin/../share/nmap 警告:未指定目标,因此扫描了 0 个主机。Nmap 完成:在 0.03 秒内扫描了 0 个 IP 地址(0 个主机)”我不明白我在做什么我做错了 - 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 2020-06-23
  • 2018-05-08
  • 2017-06-29
  • 2012-01-31
  • 2012-03-02
  • 1970-01-01
相关资源
最近更新 更多