码迷,mamicode.com
首页 > 编程语言 > 详细

盘点服务器配置信息的python脚本

时间:2018-01-15 14:49:51      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:paramiko   join()   swa   main   syn   lis   cal   uniq   rgs   

#脚本作用:查询服务器配置信息
#用法:IP写入当前文件host.txt中再执行该脚本
#日期:2018-1-15
#作者:¥¥¥

import paramiko
from multiprocessing import Pool

def main(hostname):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname,22,"用户名","密码")
except:
with open("/tmp/error.txt",‘a‘) as f:
f.write("%s is connect error\n" % hostname)
return
Cpu_num = ssh.exec_command(‘cat /proc/cpuinfo | grep "physical id" | uniq | wc -l‘)[1].read().strip()
Cpu_code = ssh.exec_command("cat /proc/cpuinfo | grep ‘cpu cores‘ | uniq")[1].read().strip()
Mem = ssh.exec_command(‘cat /proc/meminfo | grep MemTotal‘)[1].read().strip()
Disk = ssh.exec_command("""df -m|awk ‘$4~/^[0-9]/ {split($4,array,"[A-Z]");b+=array[1]} END {print b/1024}‘""")[1].read().strip()
Os = ssh.exec_command("cat /etc/issue | xargs")[1].read().strip()
Ntp_PS = int(ssh.exec_command("ps -ef | grep -v grep | grep ntp | wc -l")[1].read())
if Ntp_PS == 1:
NtpPS = "TRUE"
else:
NtpPS = "False"
Ntp = ssh.exec_command("cat /etc/ntp.conf | grep server | xargs")[1].read().strip()
Timezone = ssh.exec_command("cat /etc/timezone")[1].read().strip()
Swapiness = ssh.exec_command("cat /etc/sysctl.conf | grep swappiness | xargs")[1].read().strip()
Dns_resolv = ssh.exec_command("cat /etc/resolv.conf | grep nameserver | xargs")[1].read().strip()
Dns = ssh.exec_command("cat /etc/network/interfaces | grep dns")[1].read().strip()
Repo = int(ssh.exec_command("cat /etc/apt/sources.list | grep uledns |wc -l")[1].read())
if Repo >= 3:
Repo = "True"
else:
Repo = "False"

Zabbix = int(ssh.exec_command("ps -ef |grep zabbix |wc -l")[1].read())
if Zabbix >= 3:
    Zabbix = "True"
else:
    Zabbix = "False"

with open("/tmp/zhoujinlong.txt",‘a‘) as z:
    z.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %
            (hostname,Cpu_num,Cpu_code,Mem,Disk,Os,Ntp_PS,Ntp,Timezone,Swapiness,Dns_resolv,Dns,Repo,Zabbix))

if name == ‘main‘:
pool = Pool(processes=10)
f = open("host.txt")
for i in f:
hostname = i.strip()
pool.apply_async(main,(hostname,))
pool.close()
pool.join()

盘点服务器配置信息的python脚本

标签:paramiko   join()   swa   main   syn   lis   cal   uniq   rgs   

原文地址:http://blog.51cto.com/12629984/2061109

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!