说明:
1)下面这段Python脚本在 3.7.3版本的python环境下编写和测试通过;
2)paramiko需要安装依赖组件(如下图所示),具体安装方法可以在网上查询;
3)将不同远程Linux主机的IP地址、端口、用户名和密码放在一个cfg配置文件中,在python脚本中对ssh.connect()进行for循环,如果脚本所在主机恶=和被巡检主机之间路由态度是通的,即可实现批量巡检。
#!/usr/bin/env python3.7.3 import paramiko ssh = paramiko.SSHClient() stdin, stdout, stderr = ssh.exec_command('cat /etc/hosts| grep `hostname`|awk \'{print $1}\'') stdin, stdout, stderr = ssh.exec_command('cat /etc/redhat-release') stdin, stdout, stderr = ssh.exec_command('uname -r') stdin, stdout, stderr = ssh.exec_command('uptime| awk \'{print $2,$3,$4}\'') stdin, stdout, stderr = ssh.exec_command('df -h| sort -rn -k 5n') stdin, stdout, stderr = ssh.exec_command('cat /proc/meminfo| grep MemTotal| awk \'{print $2$3}\'') stdin, stdout, stderr = ssh.exec_command('cat /proc/meminfo| grep MemFree| awk \'{print $2$3}\'') stdin,stdout,stderr=ssh.exec_command('vmstat 1 3|sed \'1d\'|sed \'1d\'|awk \'{print $15}\'') stdin, stdout, stderr = ssh.exec_command('ps -ef| grep java| egrep \'tomcat|weblogic.Server\'| grep -v grep') stdin, stdout, stderr = ssh.exec_command('for pid in `ps -ef| egrep \'tomcat|weblogic.Server\'| grep java| grep -v grep| awk \'{print $2}\'`; do echo "pid="$pid;/usr/jdk1.7.0_191/bin/jstat -gc $pid 1000 5; done') stdin, stdout, stderr = ssh.exec_command('for pid in `ps -ef| egrep \'tomcat|weblogic.Server\'| grep java| grep -v grep| awk \'{print $2}\'`; do echo "pid="$pid", open files:" `lsof -p $pid| wc -l`; done') stdin, stdout, stderr = ssh.exec_command('ulimit -n') ssh.close() |