python3 基础脚本集合

学习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()

 

练习脚本 分享大家  关注我 每周更新脚本

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值