#脚本作用: Querying server configuration information
#用法: IP is written to the current file host.txt the script is executed again
#日期: 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, "username", "password")
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 script to Inventory server configuration information