shell ping 命令
-
shell ping 命令行参数
- -c 数据报数量
for ip in 'cat ips.txt' do if ping $ip -c 2 &> /dev/null then echo "$ip is alive" else echo "$ip is unreachable" fi done
-
python 并发ping命令
- 多线程
import subprocess import threading def is_reacheable(ip): if subprocess.call(['ping', '-c', '1', ip]): print('{} is alive'.format(ip)) else: print('{} is unreacheable'.format(ip)) def main(): with open('ips.txt') as f: lines = f.readlines() threads = [] for line in lines: thr = threading.Thread(target=is_reacheable, args=(line,)) for thr in threads: thr.join() if __name__ == '__main__': main()
- 控制线程数
import subprocess import threading from Queue import Queue from Queue import Empty def call_ping(ip): if subporcess.call(['ping', '-c', '1', ip]): print('{} is alive'.format(ip)) else: print('{} is unreachable'.format(ip)) def is_reacheable(q): try: while True: ip = q.get_nowait() call_ping(ip) except Empty: pass def main(): q = Queue() with open('ips.txt') as f: for line in f: q.put(line) threads = [] for i in range(10): thr = threading.Tread(target=is_reacheable, args=(q,)) thr.start() threads.append(thr) for thr in threads: thr.join() if __name__ == '__main__': main()
端口扫描
- socket
import socket s = socket.socket() s.connect(('www.baidu.com', 80)) s.send('GET / HTTP/1.0 \r\n') print(s.recv(200)) s.close()
import socket def conn_scan(host, port): conn = socket.socket(AF_INET, SOCK_STREAM) try: conn.connect(host, port) except Exception as e: print(host, port, 'is not avaliable') finally: conn.close() def main(): host = '127.0.0.1' for port in rang(20, 5000): conn_scan(host, port) if __name__ == '__main__': main()
- telnet 模块
import telnetlib def conn_scan(host, port): t = telnetlib.Telnet() try: t.open(host, port, timeout=1) print(host, port, 'is avaliable') except Exception as e: print(host, portm ,'is not abaliable') finally: t.close() def main(): host = '127.0.0.1' for port in range(20, 5000): conn_scan(host, port) if __name__ == '__main__': main()
- nmap
- python-nmap 是 python 对 nmap 命令的封装
- nmap 使用
-sL 仅打印主机列表 -sP -sn 不要进行端口扫描,仅判断主机是否可达 没有命令行参数会判断可达,并扫描端口 -p 指定端口号或端口号范围 -sV 确定开放端口运行的应用程序及版本信息 -sO 识别操作系统 nmap -sL 192.168.0.0/30 nmap -sL 192.168.0.101 192.168.0.102 192.168.0.103 nmap -sL 192.168.0.* --exclude 192.168.0.100 nmap -sL 192.168.0.101,102,103 nmap -sL 192.168.0.101-110
- python-nmap
import nmap nm = nmap.PortScanner() nm.scan('127.0.0.1', '22-1000') # scan 后 nm 对象的其它方法可以获取本次扫描信息 nm.command_line() nm.scaninfo() nm.all_hosts() # 通过键来查询 nm['127.0.0.1'].state() nm['127.0.0.1'].all_protocols() nm['127.0.0.1']['tcp'].keys() nm['127.0.0.1']['tcp'][80] # 手动指令 nm.scan(hosts='127.0.0.1', atguments='-n -sP -PE -PA21,23,80,3389')
使用 ipy 进行 ip地址管理
- 第三方开源,手动安装
pip install ipy
- ipy 使用
# ipy 有一个 IP类,可以接受几乎任何格式的IP地址和网段 from IPy import IP IP(0x7f000001) IP('127.0.0.1') IP('127.0.0.1/30') IP('1080:0:0:0:8:800:200C:417A') # ipv6 IP('127.0.0.0-127.255.255.255') ip.version() ip.len() ip.iptype() ip.int() ip.strHex() ip.strBin() # ip 地址存入数据库时,可转换为数字后再存入,加快存取速度,IP地址存储的最佳实践 # 网段管理 ips = IP('10.166.224.144/28') ips.len() # 16 for ip in ips: print(ip) ips.strNormal(0) # 接受 0 1 2 3 四个参数 ips.netmask() ips.broadcast() ips.overlaps('10.166.224.0/28')
使用 dnspyhton 解析 DNS
dns.resolver.query(qname, tdtype=1, rdclass=1, tcp=False, source=None, raise_on_no_answer=True, source_port=0)
# 参数shuoming
# qname 查询的域名
# rdtype 指定rr资源
- A 地址记录, 返回域名指向的ip地址
- NS 域名服务器记录,返回保存下一级域名信息的服务器地址
- MX 邮件记录
- CNAME 规范名称记录,别名记录,实现域名间映射
- PTR 逆向查询记录
# rdclass 网络类型
# tcp 是否启用 TCP 协议
# source_port 查询源的端口
# raise_on_no_answer
网络嗅探器 Scapy
-
安装
pip install scapy
生成图形化示意图需要安装 matplotlib
-
基本使用
# scapy 交互模式 # 本质 是 python 交互 from scapy.all import * print(ls()) # ls 函数列出了scapy 支持的所有协议 lsc() # 所有命令 ls(ARP) # 查看协议信息格式 # 发送报文 p1 = IP(dst='10.166.224.14') p2 = IP(dst='10.166.224.14')/TCP(dbport=80) p3 = IP(dst='10.166.224.14')/ICMP() p1.display() # 查看数据报内容 p1.dst p1.ttl = 32 del p1.ttl # 删除某个参数,参数便恢复默认值 # 构造dns查询请求 dns = DNS(rd=1, qd=DNSQR(qname='www.baidu.com')) p = sr1(IP(dst='8.8.8.8')/UDP()/dns) p[DNS].show()