前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 实用脚本

python 实用脚本

作者头像
py3study
发布2020-01-13 12:26:07
8790
发布2020-01-13 12:26:07
举报
文章被收录于专栏:python3python3

1.用python实现一个查看某网段所有主机的状态(3秒实现)

#vim ping.py

import subprocess import threading def ping(host):     result = subprocess.call(         'ping -c2 %s &> /dev/null' % host,         shell=True     )     if result == 0:         print "%s:up" % host     else:         print "%s:down" % host if __name__ == '__main__':     ips = ['172.40.55.%s' % i for i in range(1, 255)]     for ip in ips:         t = threading.Thread(target=ping, args=(ip,))         t.start()

[root@room1pc01 桌面]# python mtping.py 172.40.55.1:up 172.40.55.66:up 172.40.55.6:down 172.40.55.114:up 172.40.55.2:down 172.40.55.3:down 172.40.55.115:up 。。。。。

2.利用ssh实现多线程并发访问(可以同时创建删除,该密码等)

[root@room1pc01 ~]# yum install -y python-paramiko

#vim allhost.py

import getpass import os import paramiko import sys import threading def remote_comm(host, passwd, comm, user='root'):     ssh = paramiko.SSHClient()     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())     ssh.connect(host, username=user, password=passwd)     stdin, stdout, stderr = ssh.exec_command(comm)     out = stdout.read()     err = stderr.read()     if out:         print '[out]%s:\n%s' % (host, out),     if err:         print '[error]%s:\n%s' % (host, err),     ssh.close() if __name__ == '__main__':     if len(sys.argv) != 3:         print "Usage: %s ipfile 'command'" % sys.argv[0]         sys.exit(1)     if not os.path.isfile(sys.argv[1]):         print "No such file:", sys.argv[1]         sys.exit(2)     ipfile = sys.argv[1]     command = sys.argv[2]     pwd = getpass.getpass()     with open(ipfile) as fobj:         for line in fobj:             ip = line.strip()             t = threading.Thread(target=remote_comm, args=(ip, pwd, command))             t.start()

#vim ipaddr.txt

192.168.4.1 192.168.4.2 192.168.4.3 192.168.4.4

代码语言:javascript
复制
[root@room1pc01 桌面]# python remote_comm.py ipaddr.txt tedu.cn 'useradd bob'
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档