使用exec U命令执行多个命令

2024-09-30 10:32:16 发布

您现在位置:Python中文网/ 问答频道 /正文

登录到远程计算机后,我想“cd”到特定目录,并使用paramico模块在“exec_command”中执行命令。但无法做到这一点。我在两个命令之间使用“;”,但仍然无法获得预期的输出。在

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("10.73.73.242", username="root", password="dangerous")
print 'running remote command'
stdin, stdout, stderr = ssh.exec_command("cd /usr/ABC/PGMS;pwd")
x = stdout.readlines()
print "VALUES",x
z=stderr.readlines()  
print "err are",z
stdin, stdout, stderr = ssh.exec_command("ls")
x = stdout.readlines()
print "VALUES ARE AGAIN",x
ssh.close()

这里我想把dir改为“/usr/ABC/PGMS”执行“pwd”,但它不起作用。我不知道自己做错了什么。在

输出如下

running remote command

VALUES ['/root\n']

err are ['bash: line 0: cd: /usr/ABC/PGMS/: No such file or directory\n']

VALUES ARE AGAIN ['Desktop\n', 'Documents\n', 'autoinst.xml\n', 'bin\n', >'dup_rm_ordr.py\n', 'fullwrite.log\n', 'inst-sys\n', 'pexpect-2.3\n', >'pexpect-2.3.tar.gz\n', 'read_print_matrix.py\n', 'remove_dup.py\n', >'runlog\n']

“pwd”仍然显示/root目录并列出/root中的文件和目录。在


Tags: 目录paramikousrstderrstdoutpwdcdroot

热门问题