简体   繁体   English

如果在python shell中手动输入命令,则paramiko脚本可以正常工作

[英]paramiko script works if commands are entered manually in python shell

I have a simple python script which uses paramiko module to ssh login to a PC and turn it off. 我有一个简单的python脚本,它使用paramiko模块ssh登录到 PC并将其关闭。

The problem is the script doesn't work, whereas if the commands are typed manually into the python interpreter, the commands work as intended, the remote PC shuts down. 问题是脚本不起作用,而如果命令是手动输入python解释器,命令按预期工作,远程PC关闭。

here is my code 这是我的代码

#!/usr/bin/python3

import paramiko, subprocess
remote_ip=''
remote_user=''
remote_password=''
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(remote_ip, username = remote_user, password = remote_password,look_for_keys = False, allow_agent = False)
stdin, stdout, stderr = ssh.exec_command('sudo poweroff', get_pty = True)
stdin.write(remote_password+'\n')
stdin.flush()
ssh.close()
print(stdout.readlines())
print(stderr.readlines())

I figured out the solution, but i don't know why it works. 我想出了解决方案,但我不知道它为什么会起作用。 I removed the ssh.close() line, and now the script works as intended. 我删除了ssh.close()行,现在脚本按预期工作。 It will be helpful if someone could explain this. 如果有人能够解释这一点会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM