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

#脚本作用:查询服务器配置信息

#用法: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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值