0: Project Purpose
is an operation and maintenance Automation management project:
to reduce human intervention, reduce personnel costs --- asset Management --operations management
Avoid people to operate the server directly, use the background to unify operation
One: Implementation method (a) agent based on the shell command implementation (on the server to install the agent, the server local timing automatically to get information, sent to the database, and then the background to obtain data for processing)
Note: In general, we do not directly transfer data directly to the database, the data will be passed to the API interface for processing, filtering before it is sent to the database.
Note: Data is actively sent by the server Agent to the API
Implementation scenarios:
Executes the cmd command locally. method One: Os.system ( " command " method Two: subprocess module, With the Process Execution command, you can get to the data Popen (
def agent (self,cmd): import subprocess try: = Subprocess.getoutput (cmd) except attributeerror: = subprocess. Popen (args=cmd,shell=true,stdout=subprocess. PIPE) sub.wait () = sub.stdout.read () return ret
Python Implementation AgentAdvantages: Fast information collection, from the server to collect information to the APIdisadvantage: Agent must be installed on each server(b) SSH method: Using the Paramiko module, through the central control machine Server Unified to obtain the information of the specified server.
Paramiko Module Understanding
def SSH (self,cmd): Import Paramiko #1. Create an SSH object ssh=Paramiko. Sshclient () #2Add this sentence without worrying about choosing Yes, it will automatically pick up #3when connecting to a remote host with SSH, the first time you connect, you will be prompted to continue the remote connection, select Yes Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (hostname="Remote Host name", port="Remote Port", username="User name", password="Password") #执行命令, get results into standard input \ out \ Error stream Stdin,stdout,stderr=Ssh.exec_command (cmd) #4. Get command Results result=Stdout.read () #5. Close connection ssh.close ()
paramiko Implementing Remote Command Execution (method one: Using user name, password)
def SSH (self,cmd): Import Paramiko #1. Create an SSH object ssh=Paramiko. Sshclient () #2Add this sentence do not worry about choosing Yes, will automatically select the #用ssh连接远程主机时, the first connection will be prompted whether to continue the remote connection, choose Yes Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #3. Get the private key Private_key= Paramiko. Rsakey.from_private_key_file ("file: Where the private key is saved for decrypting") #4connect the remote server via the private key (provided that your public key is already in the other's Authorized_keys file, Paramiko has been implemented) Ssh.connect (hostname="Remote Host name", port="Remote Port", username="User name", pkey="private key Private_key") #5execute command, get results into standard input \ out \ Error stream Stdin,stdout,stderr=Ssh.exec_command (cmd) #6. Get command Results result=Stdout.read () #7. Close connection ssh.close ()returnResult
Paramiko using the private key to log on to the remote server to execute commandsPros: No need to install software such as Agent for serverCons: Slow speed
Python---CMDB Configuration management database