Python implements SSH remote login and command execution (Sharing), pythonssh
In AutomationTestIn the process, the common operation is to operate the remote host. How can this operation be performed? Remotely log on to the host using SSH, and then execute the corresponding command.
UsePythonTo achieve these operations is quite simple. Below isTestCode.
The code is as follows: (code runtime environment: python27 + eclipse + pydev)
import paramiko def sshclient_execmd(hostname, port, username, password, execmd): paramiko.util.log_to_file("paramiko.log") s = paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(hostname=hostname, port=port, username=username, password=password) stdin, stdout, stderr = s.exec_command (execmd) stdin.write("Y") # Generally speaking, the first connection, need a simple interaction. print stdout.read() s.close() def main(): hostname = '10.***.***.**' port = 22 username = 'root' password = '******' execmd = "free" sshclient_execmd(hostname, port, username, password, execmd) if __name__ == "__main__": main()
The above Python method for implementing SSH remote login and executing commands (Sharing) is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.