"Introduction to the Environment"System environment: Linux + Python 2.7.10 (monitor host)
"Background description"Requirements: Every holiday or important time, you need to check the database host information, such as host space utilization. Sometimes you need to perform database-specific command query information, such as query database patch information. However, due to the host more need to log on every time the host to query, it takes a long time. To improve bulk queries or execute commands to query the information at the host command level, you need to use a script to improve efficiency.
"Monitoring and optimization process and ideas"For the problem described above:
- Based on the execution of the command requires a bulk login host, for security considerations decide to use Python for bulk login host, need crypto and Paramiko module module
The Paramiko,crypto module follows the SSH2 protocol and supports the connection of remote servers in an encrypted and authenticated manner. Installing the Pycrypto module requires the installation of the GCC dependency package: yum-y Install Gccwget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/ PYCRYPTO-2.6.TAR.GZTAR-XF PYCRYPTO-2.6.TAR.GZCD pycrypto-2.6/python setup.py build && python setup.py installInstalling Paramiko Module installation error requires installation of Setuptools module: wget https://pypi.python.org/packages/2b/27/ B64860e7b208ff1dd36fe208d07bca1f9637a11fe733e2f2ceea587c3f75/paramiko-1.7.5.zipunzip PARAMIKO-1.7.5.ZIPCD Paramiko-1.7.5python setup.py build && python setup.py installtraceback (most recent call last): File "setup.py", Line 6, in <module> from Setuptools Import Setup, Find_packagesimporterror:no module named Setuptools installation Setuptoo LS module: https://pypi.python.org/pypi/setuptoolsunzip setuptools-38.5.1.zipcd setuptools-38.5.1python setup.py build && python setup.py installTest Paramiko and crypto modules for normal invocation: [[email protected] python2.7.10]$ Pythonpython 2.7.10 (default, Nov, 20:27:52) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on Linux2type ' help ', ' copyright ', ' credits ' or ' license ' for more information.>>> Import Crypto>>> Import Paramiko>>>
- Host acquisition of information, self-naming does not need to set, call other user commands need to set environment variables, the need to execute the command directly into the profile configuration file.
Set Variable: path=/home/oracle/opt/python2.7.10/bin: $ORACLE _home/jdk/bin: $ORACLE _home/opatch: $PATHexport PATH
"Test results and Scripts"Pass the test script. The results are as follows:
- Host data information: The following:
Implement the interactive Execution Host command, enter the command and return directly. Can be any host executable command.
- Database information: The following:
Pre-load the user's profile file configuration after executing the command: source ~/.bash_profile;~/.profile;opatch version
"Collect Data script information"The main implementation features of the script are as follows: [[email protected] python]$ cat ssh_hosts.sh########################################################## ############# ssh_hosts.sh# This script is check host data# Author czt################################################# ######################!/bin/bashcat. Liebiao.txt |awk ' {print $ ' |grep-v ' hostip ' > tmp_ipadress.txt ' load IP information cat. " Liebiao.txt |awk ' {print $4} ' |grep-v ' PASSWORD ' > tmp_password.txt ' load password information python ssh_hosts.py"" call Python script bulk Login host execute command [[email protected] python]$ cat. liebiao.txt########################################################## ############# liebiao.txt# This script is hosts data# Author czt###################################################### ############### #TYPE dbname username PASSWORD hostipcrm yydb1 oracle oracle 192.168.142.140crm yydb1 oracle oracle 192.168.142.140crm ggdb1 oracle Oracle 192.168.142.140crm ggdb2 oracle oracle 192.168.142.140"" security considerations can be used to temporarily write to the list information file, the host information can be added yourself[email protected] python]$ cat ssh_hosts.py###################################################################### # ssh_hosts.py# This script is check hosts command# Author czt######################################################### ############ #import Paramiko"" load Paramiko module# Define the IPAddress array."" load host IP information as an arraypatch_file_name1= "Tmp_ipadress.txt" infile1= Open (patch_file_name1, ' r ') ipadress = []for line1 in Open (Patch_file_ NAME1): Trainingset = line1.split (' \ n ') ipadress.append (trainingset[0]) # Define the password array."" load host password information for an arraypatch_file_name1= "Tmp_password.txt" infile1= Open (patch_file_name1, ' r ') password = []for line2 in Open (Patch_file_ NAME1): Trainingset = line2.split (' \ n ') password.append (trainingset[0]) Username = ' Oracle '"" because they are all Oracle users, only set as unique variables, no need to set the array user informationPort = 22" because the host is called Port 22nd, only set as a unique variablenum = 0" because it involves looping the execution of commands, set num initial variable to 0 as the circular reference values = Paramiko. Sshclient ()" Call the SSH function in the Paramiko moduleS.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())" Security to Telnet to the host server in an encrypted and authenticated mannerSshcmd=raw_input ("Enter your Sql_text:");set an interactive command variable, which can be any variable command that the host can executeFor hostname in ipadress:"" The host information variable exists in the loopPrint ("#######################################################################################################" ) Print ("############################### IP addreess:" + str (hostname). Ljust (15) + "###########################")"" output host information as a unique identifierS.connect (hostname = hostname, port = port, username = username, password = str (password[num]))"" " Login hoststdin, stdout, stderr = S.exec_command (sshcmd)" Call the host command above for interactive executionPrint Stdout.read () print stderr.read () output execute command message num + = 1s.close ()"" closes the SSH function in the Paramiko module
"Problem thinking"
- There is a system version limit, need to install multiple modules;
- There is a database version limit, if you call the database command, you need to set environment variables;
- There are security restrictions, user and password information needs to be manually cleaned up in time, can manually load the host information file.
"Summary"
- Realize interactive in multiple hosts need to check the host command level information, improve the efficiency of information query is very obvious.
- This script involves invoking the variables of the Oracle user, which has a greater requirement for the user's variable setting criteria.
Capture Python scripting for multiple server interactive host commands at once