0
recv_ready()
은 올바른 값을 반환하지 않지만 paramiko를 사용하여 원격으로 많은 명령을 실행하려고합니다.
예를 들어, pwd \n
명령 후에 채널이 아직 준비되지 않았 음을 계속보고합니다 (분명히 거짓). 일부 명령의 경우 제대로 작동합니다. ls
. 내가하고있는 일에 어떤 문제가 있습니까? 아니면 paramiko에 문제가 있습니까? 채널의 데이터를 읽거나, 즉 데이터가 버퍼링되지 않거나없는 경우 준비paramiko recv_ready() false 값을 반환합니다.
import paramiko
import re
import time
def sudo_ssh(hostname, usernameIn, passIn, cmd):
# Create an SSH client
client = paramiko.SSHClient()
# Make sure that we add the remote server's SSH key automatically
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the client
client.connect(hostname, username=usernameIn, password=passIn)
# Create a raw shell
channel = client.invoke_shell()
# Send the sudo command
for command in cmd:
print("CMD= " + command + "\n")
time.sleep(1)
# wait until channel is ready
while not channel.recv_ready() :
print("NOT READY " + str(channel.recv_ready()) + "\n \n")
time.sleep(1)
# Send the command
channel.send(command)
channel.send("\n")
# Wait a bit, if necessary
time.sleep(1)
# Flush the receive buffer
receive_buffer = channel.recv(4096)
# If promted send the sudo pass
if re.search(b".*\[sudo\].*", receive_buffer):
time.sleep(1)
print(" TYPING SUDO PASSWORD .... \n")
channel.send("sudoPass" + "\n")
receive_buffer = channel.recv(4096)
# Print the receive buffer, if necessary
print(receive_buffer)
print("Executed all of the commands. Now will exit \n")
client.close()
com = []
com.append("sudo ls")
com.append("cd /home/user/Downloads")
com.append("sleep 5")
com.append("ls")
com.append("pwd")
com.append("cd /opt/")
sudo_ssh("myhost.com", "user", "pass", com)