時間:2020-04-23來源:電腦系統城作者:電腦系統城
代碼實例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# -*- coding:utf-8 -*- import paramiko class ssh( object ): def __init__( self ,host,port,user,password): self .host = host self .port = port self .user = user self .password = password self .ssh_client = paramiko.SSHClient() self .ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self .ssh_client.connect( self .host, self .port, self .user, self .password) #執行指令返回文本字符串 def sftp_exec_command( self ,command): arrconfiglist = [""] try : std_in, std_out, std_err = self .ssh_client.exec_command(command) for line in std_out: arrconfiglist.append(line.strip( "\n" )) del arrconfiglist[ 0 ] self .ssh_client.close() return arrconfiglist except Exception as e: print (e, "ssh ERROR" ) finally : self .ssh_client.close() #執行指令無返回 def sftp_exec_norecommand( self ,command): try : self .ssh_client.exec_command(command) self .ssh_client.close() except Exception as e: print (e, "ssh ERROR" ) finally : self .ssh_client.close() ''' 在別的項目中被調用使用如下方法 import ssh as ssh if __name__ == '__main__': ssh.ssh().sftp_exec_command("--command information--") ''' ''' if __name__ == '__main__': rect = ssh().sftp_exec_command("") print(rect) ''' |
2022-03-01
PHP如何從txt文件中讀取數據詳解2022-03-01
分享5個方便好用的Python自動化腳本2021-03-29
Python中pycharm編輯器界面風格修改方法