Small test sledgehammer Python implementation of bulk acquisition of host-related data

Source: Internet
Author: User

I. Overview of the scene

On the 3rd day of the new company, I received a mission to get information about more than 100 physical servers, including: hostname, IP address, number of disks, disk device number. The sad reminder is that the existing production environment does not have any tools to do this, I would like to use ansible to do, but because of ansible is not very familiar with, so decided to hand-off a script to achieve, so it is the birth of this script.

Second, the basic idea of the script

1, Python pexpect module, using this module to implement SSH connection

2, after the connection in the remote host run related commands

3. Return results

4, return the results of whatever you do, visualization or, analysis, storage to the database is also OK

Third, the code is as follows

#!/usr/bin/env python#describe: #Gets  the physical hard disk number of  Each host#author: tantianran#email:[email protected] #time:2016-10-09 17:28# -*-  Coding: utf-8 -*-import pexpectdef ssh_cmd (ip,shell_cmd):p asswd =  ' 1qaz#EDC ' ssh  = pexpect.spawn (' ssh [email protected]%s  '%s '  %  (ip, shell_cmd)) Try: I = ssh.expect ([' Password: ',  ' continue connecting  (yes/no)? '], timeout=5 ') if  i == 0 :ssh.sendline (passwd) elif i == 1:ssh.sendline (' yes\n ') ssh.expect (' password:  ') ssh.sendline (passwd) ssh.expect (' # ') ssh.sendline (shell_cmd) ssh.expect (' # ') except pexpect. eof:data = ssh.before     #读取shell命令运行的结果ssh. Close () Except pexpect. timeout:print  "Connect timeout ..." ssh.close () return data    # Return shell command Run result Def get_host (): Cmd =  ' name= ' hostname ' ip= ' ifconfig eth0 | grep inet|grep -v 127.0.0.1|grep  -v inet6|awk  ' {print $2} ' disk= ' Ls -l /dev/vd[a-z] | wc -l ' list= ' ls -l /dev/vd[a-z] | awk  ' {print $10} ' echo  ' hostname: ' $name   IP ' $ip   "Disk:" $disk   "info:" $list "Return cmdfor i in range (165,167): ipaddr =   ' 192.168.122.%s '  % idata = ssh_cmd (Ipaddr,get_host ()) print dataf =  Open (' Host_info.txt ', ' a ') f.write (data)

Iv. Description of test environment

At the time of writing this script, two VMs were created to simulate a scene in the production environment. vm01:192.168.122.165,vm02:192.168.122.166

This article is from "My Ops and my original" blog, so be sure to keep this source http://trtan.blog.51cto.com/8272891/1862140

Small test sledgehammer Python implementation of bulk acquisition of host-related data

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.