标签:sof .exe == lin mis logs ram ddp 基于
paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令、文件传输等功能。
paramiko模块不是python自带的标准库,执行pip install paramiko进行安装。如果pip未配置,请参考<python工具安装和pip工具配置>完成配置。
1、在远程服务器执行指定命令
import paramiko def ssh_con(ip, username, password): try: con = paramiko.SSHClient() con.set_missing_host_key_policy(paramiko.AutoAddPolicy()) con.connect(ip=ip, username=username, password=password, timeout=5) except: return None else: return con def exec_cmd(conn, cmd): interact = conn.invoke_shell() interact.send(cmd) result = interact.recv(65535) return result def exec_cmd2(conn,cmd): stdin, stdout, stderr = conn.exec_command(cmd) channel = stdout.channel ret = channel.recv_exit_status() if ret == 0: return (ret, stdout.read()) else: return (ret, stderr.read()) if __name__ == ‘__main__‘: ip = ‘192.168.0.1‘ user = ‘root‘ passwd = ‘root‘ con = ssh_con(ip, user, passwd) if not con: print "Connect %s fail,please check." % (ip,) cmd = ‘df -k‘ ret, result = exec_cmd2(con,cmd) print result
标签:sof .exe == lin mis logs ram ddp 基于
原文地址:https://www.cnblogs.com/linyfeng/p/8964753.html