学习python测试出来的一些脚本 分享给大家
1. 服务器状态检测(shell)
#!/bin/bash
# 检测服务器是否在线
cat ./scan.sh
for ip in `cat /root/ips.txt`
do
if ping $ip -c 2 &> /dev/null
then
echo "$ip is alive"
else
echo "$ip is unreachable"
fi
done
2. 服务器状态检测结合shell指令(python3)
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import subprocess
import threading
def is_reacheable(ip):
if subprocess.call(["ping","-c", "3", 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:
print('-------\n开始检测:',line)
thr = threading.Thread(target=is_reacheable(line,))
thr.start()
threads.append(thr)
for thr in threads:
thr.join()
if __name__ == '__main__':
main()
3. 使用python的socket模块对服务器端口连通性进行测试(python3脚本)
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from socket import *
def conn_scan(host, port):
conn = socket(AF_INET, SOCK_STREAM)
try:
conn.connect((host, port))
print(host, port, 'is avaliable')
except Exception as e:
print(host, port, 'is not avaliable')
finally:
conn.close()
def main():
host = "47.106.113.221"
for port in range(20,5000):
conn_scan(host, port)
if __name__ == '__main__':
main()
4. 使用python的telnetlib模块对服务器端口进行检测(python3)
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
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, port, 'is not avaliable')
finally:
t.close()
def main():
host = '47.106.113.221'
for port in range(20,100):
conn_scan(host, port)
if __name__ == '__main__':
main()
练习脚本 分享大家 关注我 每周更新脚本