python - 带有 Yes No 选项的 Python3 nmap 脚本
问题描述
我是 Python3(和 Python 2.x)的新手,我经常使用 nmap 定期扫描我的服务器,以确保正确的端口是打开或关闭的。
我的目标是编写一个带有 nmap 的 Python3 脚本,供其他 IT 人员使用。我希望我的脚本执行以下操作:
选择要扫描的选项
option 1 quick scan option 2 most common tcp ports option 3 scan ports 1-6000
询问用户是否愿意将扫描结果写入输出文件,或者只是运行扫描并从终端读取输出。
输入要扫描的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
解决方案
#!/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 库以非常有效的方式完成它,而代码行数更少。
您的参考链接。
推荐阅读
- java - 如何更改“given()”链中前面给出的放心设置?
- javascript - 用空白页反应生产构建结果。开发服务器工作
- apache-nifi - 带有 Google Flatbuffers 有效负载的 Websocket 流的 Apache NiFi
- jsp - Java 8,JSP - 应该将 Optional 对象放在 request.setAttribute() 或 session.setAttribute() 中吗?
- sql - Why SQL query with SHA2_256 returns NULL in WHERE clause
- coinbase-api - 开发者可以控制他们的私钥吗?
- powershell - Get-ADObject 的两行标题 | 导出-CSV?
- azure - 无法使用 Azure 函数核心工具在本地运行 cosmosDB 触发器
- php - Validating the key in the data in Lumen
- ruby - 尝试使用 RVM 安装 Ruby 时运行“__rvm_make -j8”时出错