I. Description of the procedure
The SSH client implementation mainly has the following four issues:
The first question is which package implementation is used by the SSH client in python----we're using Paramiko.
The second question is how to connect the server----connect to the server directly using the Connect () function, there is a pit is not in the known_hosts file of the machine by default does not allow the connection need to deal with
The third question is how to execute a command after a connection----the command can be executed directly with the Exec_command () function after the connection.
The fourth question is how to read the command execution result----the Exec_command () function returns the result of the function execution and receives it with one parameter.
Our entire sequence of processes here is:
Log on to the host using the user name password----Execute the whoami command if the login succeeds----print the WHOAMI command result----exit the SSH session
Second, the program source code
ImportLoggingImportSYS fromParamikoImportauthenticationexception fromParamiko.clientImportsshclient, Autoaddpolicy fromParamiko.ssh_exceptionImportNovalidconnectionserrorclassmysshclient ():def __init__(self): self.ssh_client=sshclient ()#This function is used to enter the user name password to log on to the host defSsh_login (Self,host_ip,username,password):Try: #set allow hosts in the Known_hosts file to be connected (the default connection is not in the Known_hosts file and the host rejects the connection throw sshexception)Self.ssh_client.set_missing_host_key_policy (Autoaddpolicy ()) Self.ssh_client.connect (Host_ip,po RT=22,username=username,password=password)exceptAuthenticationException:logging.warning ('username or password error') return1001exceptNoValidConnectionsError:logging.warning ('Connect Time Out') return1002except: Logging.warning ('Unknow Error') Print("Unexpected error:", Sys.exc_info () [0])return1003return1000#This function is used to execute commands in the command parameter and Print command execution results defExecute_some_command (Self,command): stdin, stdout, stderr=self.ssh_client.exec_command (command)Print(Stdout.read (). Decode ())#Use this function to exit the login defssh_logout (self): logging.warning ('Would exit host') Self.ssh_client.close ()if __name__=='__main__': #Remote Host IPHOST_IP ='192.168.220.129' #Remote Host user nameUsername ='Root' #Remote host PasswordPassword ='Toor' #the shell command to execute, and the command you want to execute #How do I write command parameters when I use SSH?Command ='WhoAmI' #instantiation ofMy_ssh_client =mysshclient ()#log in, if the result is 1000, then execute the command, then exit ifMy_ssh_client.ssh_login (Host_ip,username,password) = = 1000: logging.warning (f"{Host_ip}-login success, 'll execute Command:{command}") My_ssh_client.execute_some_command (command) my_ssh_client.ssh_logout ()
Reference:
Http://docs.paramiko.org/en/2.4/api/client.html
Python3+paramiko Implementing an SSH Client