Python SSH module logon, remote machine execution of shell Command instance parsing, pythonssh
Use the python SSH module to log on and execute shell commands on a remote machine.
(The Redhat series should be compatible if the test is successful in CentOS 7 .)
Install required modules first
# yum install python-dev# yum install python-devel# pip install pycrypto# pip install paramiko# pip install ssh
After all these are successful, write a Python script
# Vim remote_run.pyimport ssh # create an ssh client object myclient = ssh. SSHClient () # set to automatically accept the key myclient by default. set_missing_host_key_policy (ssh. autoAddPolicy () # connect to the remote host myclient. connect ("xxx.coder4.com", port = 22, username = "xxxx", password = "xxxx") # execute shell commands stdin, stdout, stderr = client.exe c_command ("ls-l") # Read the returned result print stdout. read () # execute the python script command stdin, stdout, stderr = client.exe c_command ("python/home/test. py ")
After creating an SSHClient object, in addition to executing commands, you can also enable an sftp session for transferring files and creating folders.
# Create sftp sessionsftp = client. open_sftp () # create the directory sftp. mkdir ('abc') # download files from the remote host. If the download fails, an exception may be thrown. Sftp. get ('test. sh', '/home/testl. sh ') # uploading a file to a remote host may also throw an exception sftp. put ('/home/test. sh', 'test. sh ')
Summary
The above is all the content about the python SSH module logon and remote Machine shell Command instance parsing, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!