python 中使用 paramiko 模块,远程执行命令

 时间:2024-10-21 07:44:52

1、导入paramiko 模块#!/usr/bin/pythonimport paramiko

python 中使用 paramiko 模块,远程执行命令

2、创建 ssh 连接函数def ssh_connect( _host, _username巳呀屋饔, _password ): try: _ssh_fd = paramiko.SSHCli髫潋啜缅ent() _ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() ) _ssh_fd.connect( _host, username = _username, password = _password ) except Exception, e: print( 'ssh %s@%s: %s' % (_username, _host, e) ) exit() return _ssh_fd

python 中使用 paramiko 模块,远程执行命令

3、创建命令执行函数def ssh_exec_cmd( _ssh_fd, _cmd ): return _ssh_fd.exec_command( _cmd )

python 中使用 paramiko 模块,远程执行命令

4、创建关闭 ssh 函数def ssh_close( _ssh_fd ): _ssh_fd.close()

python 中使用 paramiko 模块,远程执行命令

5、使用示例if __name__ == '__main__': sshd = s衡痕贤伎sh_connect( '192.168.134.220', 'root', '1' ) stdin, stdout, stderr = ssh_exec_cmd( sshd, 'ls' ) err_list = stderr.readlines() if len( err_list ) > 0: print 'ERROR:' + err_list[0] exit() for item in stdout.readlines(): print item, ssh_close( sshd )

python 中使用 paramiko 模块,远程执行命令
  • Python决定它是否是一个函数
  • Unable to create .git/index.lock File exists
  • 怎么在ubuntu上安装anaconda
  • 怎么使用JDBC连接SQL Server2008
  • Linux-文件搜索命令find的使用
  • 热门搜索
    三年级重阳节手抄报 手抄报a3版面设计图 好书伴我行手抄报 我是环保小卫士手抄报 诚实守信手抄报图片 书香校园手抄报资料 三年级上册手抄报 四年级上册手抄报 国庆70周年手抄报内容 环保手抄报内容50字