- 论坛徽章:
- 13
|
本帖最后由 hmchzb19 于 2016-11-18 21:09 编辑
代码在这里:
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect('192.168.0.199',username='root',password='passw0rd')
- output=""
- err_str=""
- stdin, stdout, stderr=ssh.exec_command("/usr/bin/cp /root/names.txt /tmp/names.txt.before; echo good bye; sleep 1; ls -al /dev",)
- try:
- while not stdout.channel.exit_status_ready():
- rl, wl, xl = select.select([stdout.channel,], [], [], 0.0)
- if len(rl) != 0:
- output+=stdout.channel.recv(512).decode()
- err_str+=stdout.channel.recv_stderr(512).decode()
- except socket.timeout:
- print("Operation time out. Ouput might not be complete.")
- rc=stdout.channel.recv_exit_status()
- print(rc)
- print(output)
- print(err_str)
复制代码
这段代码经过我的测试,我发现只会输出一部分/dev/下面的文件,不知道为什么会这样,请高手指点。 主要是while not stdout.channel.exit_status_ready():
rl, wl, xl = select.select([stdout.channel,], [], [], 0.0)
if len(rl) != 0:
output+=stdout.channel.recv(512).decode()
err_str+=stdout.channel.recv_stderr(512).decode()
这里的代码我感觉不知道怎么写。
|
|