1、http://t.zoukankan.com/xiamaojjie-p-13385223.html
执行多条linux命令,多条命令放在一个单引号下面,各命令之间用分号隔开,如:cmd_str = “cd /;cd etc;ls -l;” 且在末尾加上get_pty=True
demo如下:
import paramiko def remot_connect(hostname, port, username, password, cmd_str): # 创建SSHClient 实例对象 ssh = paramiko.SSHClient() # 调用方法,表示没有存储远程机器的公钥,允许访问 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接远程机器,地址,端口,用户名密码 ssh.connect(hostname, port, username, password, timeout=10) # 输出命令执行结果 stdin, stdout, stderr = ssh.exec_command(cmd_str, get_pty=True) result = stdout.read() print(result.decode()) # 关闭连接 ssh.close() if __name__ == '__main__': hostname = "121.196.120.113" port = 22 username = "root" password = "123_Xiaohaikun" cmd_str = "cd /;cd etc;ls -l;" remot_connect(hostname, port, username, password, cmd_str)
代码运行结果如图:
xshell上显示如图:
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288469.html