用python写一个批量扫描ip的脚本
时间: 2023-02-25 12:07:38 浏览: 190
您好,以下是用 Python 写一个批量扫描 IP 的脚本的简单示例:
```
import socket
ips = ['192.168.0.1', '192.168.0.2', '192.168.0.3']
for ip in ips:
try:
socket.inet_aton(ip)
print(f"{ip} is valid")
except:
print(f"{ip} is invalid")
```
这个脚本通过使用 `socket.inet_aton` 函数检查 IP 地址是否有效,并在输出中报告 IP 地址的有效性。此代码仅供参考,实际应用中可能需要根据您的具体需求进行更多的扩展和修改。
相关问题
使用python编写一个批量扫描端口存活的脚本
当然可以,下面是一个示例的Python脚,可用于批量扫描端的存活情况:
```python
import
from concurrent.futures import ThreadPoolExecutor
def_port(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket_STREAM)
sock.settimeout(2)
= sock.connect_ex((ip, port))
if result == :
print(f"Portport} is open on {ip}")
.close()
except Exception as e:
(f"Error occurred while scanning port {port} {ip}: {e}")
batch_scan_ports(ip,):
with ThreadPoolExecutor(max_workers10) as executor:
for port in ports:
.submit(scan_port, ip,)
if __name__ ==main__":
ip =127.0.0.1" # 替换为你要扫描的IP地址
ports = [80, 443, 8080, 22] # 替换为要扫描的端口列表
batch_scan_ports(ip, ports)
```
在上面的示例中,我们定义了两个函数:`scan_port`用于扫描单个端口的存活情况,`batch_scan_ports`用于批量调用`scan_port`函数来扫描多个端口。我们使用线程池来并发执行端口扫描任务,以提高效率。
你需要将`ip`变量替换为你要扫描的目标IP地址,将`ports`列表替换为你要扫描的目标端口列表。然后运行脚本,它将输出每个存活的端口。
请注意,这只是一个简单的示例,可能无法处理大规模的端口扫描。如果需要进行更复杂的端口扫描任务,可以考虑使用专业的端口扫描工具。
用python写一个nmap扫描脚本
### 回答1:
好的,以下是一个使用 Python 编写的 nmap 扫描脚本示例:
```python
import nmap
# 创建 nmap.PortScanner 对象
nm = nmap.PortScanner()
# 执行扫描,扫描 localhost 的22、80、443端口
nm.scan('localhost', '22,80,443')
# 打印扫描结果
for host in nm.all_hosts():
print('Host : %s (%s)' % (host, nm[host].hostname()))
print('State : %s' % nm[host].state())
for proto in nm[host].all_protocols():
print('Protocol : %s' % proto)
lport = nm[host][proto].keys()
lport.sort()
for port in lport:
print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
```
请注意,使用 nmap 库需要安装 nmap 软件。如果您的系统上没有安装 nmap,请先安装 nmap。
### 回答2:
要使用Python编写一个nmap扫描脚本,需要使用python-nmap模块来与nmap工具进行交互。以下是一个简单的示例脚本:
```python
import nmap
# 创建一个nmap扫描器对象
scanner = nmap.PortScanner()
# 输入要扫描的目标IP地址或域名
target = input("请输入要扫描的目标IP地址或域名:")
# 执行扫描
result = scanner.scan(target, arguments='-p1-65535 -sV -sS -O')
# 输出扫描结果
print("扫描结果: ")
print("目标: %s" % result['scan'][target]['hostnames'][0]['name'])
print("状态: %s" % result['scan'][target]['status']['state'])
# 输出所有开放端口和对应的服务信息
for port in result['scan'][target]['tcp']:
print("端口: %s\t状态: %s\t服务: %s" % (port, result['scan'][target]['tcp'][port]['state'], result['scan'][target]['tcp'][port]['name']))
```
上述脚本中,我们首先导入了nmap模块,并创建了一个nmap扫描器对象。然后,通过输入函数获取要扫描的目标IP地址或域名。接下来,我们使用扫描器对象的scan方法执行扫描,并传递了一些参数('-p1-65535 -sV -sS -O')来指定扫描的端口范围和执行的扫描类型。最后,我们通过访问结果字典来输出扫描结果,包括目标信息、主机状态以及所有开放端口和对应的服务信息。
### 回答3:
编写一个Python脚本来执行nmap扫描是相对简单的。你可以使用Python的subprocess库来调用命令行,并通过subprocess.Popen函数传入nmap命令来执行扫描。
下面是一个简单的示例:
```python
import subprocess
def nmap_scan(target):
scan_result = []
# 使用subprocess.Popen调用nmap命令
proc = subprocess.Popen(['nmap', '-sS', '-p 1-1000', target], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 读取扫描结果
output, error = proc.communicate()
# 解析输出
lines = output.decode().split('\n')
for line in lines:
if 'open' in line:
scan_result.append(line)
return scan_result
# 测试脚本
target = '127.0.0.1'
result = nmap_scan(target)
print(result)
```
在这个示例中,我们定义了一个名为nmap_scan的函数来执行nmap扫描。我们使用subprocess.Popen函数传递nmap命令,并将输出和错误以字节串形式返回到变量output和error中。然后我们将输出解码为字符串并按行拆分。我们使用一个简单的条件语句来筛选出包含'open'字样的行,然后将其添加到scan_result列表中。最后,我们返回scan_result。
注意,上面的示例假定你已经安装了nmap工具,并且已将其添加到了系统的环境变量中。如果你的环境不同,你可能需要修改nmap命令的路径或其他参数。
这只是一个简单的示例,你可以根据你的需求和使用nmap命令的其他参数来进一步自定义脚本。