python SSHモジュールログイン、リモートマシンshellコマンドインスタンス解析

1418 ワード

python SSHモジュールでログインし、リモートマシンでshellコマンドを実行
(CentOS 7環境での試験に成功し、Redhatシリーズは互換性があるはずです.)
必要なモジュールを先にインストール

# yum install python-dev
# yum install python-devel
# pip install pycrypto
# pip install paramiko
# pip install ssh

これらが成功したら、Pythonスクリプトを作成します.

# vim remote_run.py
import ssh
#     ssh     
myclient = ssh.SSHClient()
#            
myclient.set_missing_host_key_policy(ssh.AutoAddPolicy())
#       
myclient.connect("xxx.coder4.com", port=22, username="xxxx", password="xxxx")
#       shell  
stdin, stdout, stderr = client.exec_command("ls -l")
#      
print stdout.read()
#       python    
stdin, stdout, stderr = client.exec_command("python /home/test.py")

SSHClientオブジェクトを作成すると、コマンドの実行に加えて、ファイルの転送、フォルダの作成など、sftpのセッションを開くことができます.

#    sftp session
sftp = client.open_sftp()
 
#     
sftp.mkdir('abc')
 
#          ,    ,          。
sftp.get('test.sh', '/home/testl.sh')
 
#          ,        
sftp.put('/home/test.sh', 'test.sh')

まとめ
以上、python SSHモジュールのログインについて、リモートマシンがshellコマンドのインスタンス解析を実行するすべての内容を説明しました.興味のある方は引き続き当駅の他の関連テーマを参照することができます.不足点があれば、伝言を歓迎します.友达の本駅に対する支持に感谢します!