gpt4 book ai didi

python - 使用python进行Nmap扫描并存储在mysql中

转载 作者:行者123 更新时间:2023-11-29 16:08:44 25 4
gpt4 key购买 nike

使用 nmap 扫描一系列 ip 的 python 代码,然后将这些 ip 单独存储在 mysql 表中。

当我们需要扫描一定范围的ip时如何扫描,例如172.27.20.130-140。并将所有ip存储在mysql表中。现在需要搜索一个范围,并且每个ip应该存储在表中。

#NMAP SCANNING CODE
nmScan=nmap.PortScanner()
host=self._firstname.value # taking input from a pyforms gui input field
result=nmScan.scan(hosts=host, arguments='-sV -v -p 1-1024')
print('Host : %s (%s)' % (host, nmScan[host].hostname()))
print('State : %s' % nmScan[host].state())
for proto in nmScan[host].all_protocols():
print('----------')
print('Protocol : %s' % proto)
lport = nmScan[host][proto].keys()
#lport.sort()
for port in lport:
thisDict = nmScan[host][proto][port]
print ('port : %s\tstate : %s\tVersion:%s,v%s'% (port, nmScan[host][proto][port]['state'],thisDict['product'] ,thisDict['version']))


#INSERT into INPUT table CODE
try:
connection = mysql.connector.connect(host='localhost',
database='testdb',
user='root',
password='Pooja@123')
cursor = connection.cursor()
sql_insert_query = """ INSERT INTO input (sno,ip) VALUES (%s,%s)"""
so=cursor.lastrowid
sno=so
ip=self._firstname.value # taking input from a pyforms gui input field
insert_tuple = (sno,ip)
result = cursor.execute(sql_insert_query, insert_tuple)
print("inserted")
connection.commit()
except mysql.connector.Error as error :
connection.rollback() #rollback if any exception occured
print("Failed inserting record into input table {}".format(error))
finally:
#closing database connection.
if(connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")

最佳答案

ipaddress 模块对于此类事情非常有用:

In [1]: import ipaddress

In [2]: addr = ipaddress.IPv4Address('172.27.20.130')

In [3]: print(addr)
172.27.20.130

In [4]: print(addr + 1)
172.27.20.131

关于python - 使用python进行Nmap扫描并存储在mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55487984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com