一键获取linux系统版本、资源、网络、进程、串口等信息
说明:
使用pyrhon 一键获取linux系统版本、资源、网络、进程、串口等信息
代码:
import paramiko
import time
def test(ip,username,password,port):
# 创建一个ssh的客户端,用来连接服务器
ssh = paramiko.SSHClient()
# 创建一个ssh的白名单
know_host = paramiko.AutoAddPolicy()
# 加载创建的白名单
ssh.set_missing_host_key_policy(know_host)
# 调用connect方法连接服务器
try:
ssh.connect(ip,port,username,password, timeout=3)
print ("\n------------------------系统--------------------------------")
try:
stdin, stdout, stderr = ssh.exec_command("pwd")
print ("\n【1】.当前位置(pwd):%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("cat /etc/issue")
print ("\n【2】.查看操作系统版本(cat /etc/issue):%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("hostname")
print ("\n【3】.计算机名(hostname):%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("uname -a")
print ("\n【4】.查看系统体系结构(uname -a):%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("uname -m")
print ("\n【5】.CPU架构(uname -m):%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("cat /proc/cpuinfo")
print ("\n【6】.查看CPU信息(cat /proc/cpuinfo):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("lsmod")
print ("\n【7】.列出加载的内核模块(lsmod):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("env")
print ("\n【8】.查看环境变量(env):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("lsusb -tv")
print ("\n【9】.列出所有USB设备(lsusb -tv):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("cat /proc/bus/input/devices")
print ("\n【10】.列出所有输入设备(cat /proc/bus/input/devices):")
print (stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("cat /proc/devices")
print ("\n【11】.系统已经加载的所有块设备信息(cat /proc/devices):")
print (stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
print ("\n------------------------资源--------------------------------")
try:
stdin, stdout, stderr = ssh.exec_command("free -m")
print ("\n【1】.内存信息(free -m):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
#fdisk -l //分区情况 , df -h //大小情况
stdin, stdout, stderr = ssh.exec_command("fdisk -l")
print ("\n【2】.硬盘信息(fdisk -l):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("df -h")
print ("\n【3】.查看各分区使用情况(df -h):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("cat /proc/loadavg")
print ("\n【4】.查看系统负载:\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
print ("\n------------------------网络--------------------------------")
try:
stdin, stdout, stderr = ssh.exec_command("ifconfig")
print ("\n【1】.查看所有网络接口的属性(ifconfig):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("iptables -L")
print ("\n【2】查看防火墙设置(iptables -L):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("route -n")
print ("\n【3】查看路由表(route -n):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("netstat -ant")
print ("\n【3】查看所有已经建立的连接(netstat -ant):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
print ("\n------------------------进程--------------------------------")
try:
stdin, stdout, stderr = ssh.exec_command("ps -ef")
print ("\n【1】.查看所有进程(ps -ef):")
print (stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("busybox top -n 1")
print ("\n【2】.实时显示进程状态(busybox top -n 1):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
print ("\n------------------------串口--------------------------------")
try:
stdin, stdout, stderr = ssh.exec_command("ls -l /dev/ttyS*")
print ("\n【1】.查看串口名称使用(ls -l /dev/ttyS*):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
try:
stdin, stdout, stderr = ssh.exec_command("dmesg | grep ttyS*")
print ("\n【2】.查看串口设备(dmesg | grep ttyS*):\n%s"%stdout.read().decode("utf-8").strip())
except Exception as e:
print ('异常:',e)
except Exception as e:
print ("连接失败:",e)
# 关闭ssh连接
ssh.close()
if __name__ == '__main__':
ip = "192.168.231.131"
username = "test"
password = "test1234"
port = 22
test(ip,username,password,port)
执行:

测试结果:



