Python script to Inventory server configuration information

Source: Internet
Author: User
Tags python script

#脚本作用: 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.