channel.recv在命令完成后仍未返回提示。’while True’循环未中断。

huangapple go评论98阅读模式
英文:

channel.recv didn't return prompt back even after command is completed. 'while True' loop didn't break

问题

  1. channel.recv在命令完成后仍未返回提示符'while True'循环未中断
  2. 我正在使用paramiko登录一个服务器用户为'xyz'然后使用paramiko通道执行'su -'
  3. ```python
  4. ssh = paramiko.SSHClient()
  5. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  6. try:
  7. ssh.connect(hostname, username='xyz', password=list_pass(hostname, 'xyz'))
  8. command = 'puppet agent -t --no-use_cached_catalog ; echo COMMAND_COMPLETED'
  9. channel = ssh.invoke_shell()
  10. channel.send("su -\n")
  11. time.sleep(2)
  12. channel.send(list_pass(hostname, 'root') + "\n")
  13. time.sleep(2)
  14. channel.send(command + '\n')
  15. time.sleep(5)
  16. while True:
  17. if channel.recv:
  18. time.sleep(1)
  19. print(channel.recv(1024).decode())
  20. if "COMMAND_COMPLETED" in channel.recv(1024).decode():
  21. break
  22. else:
  23. continue
  24. channel.close()
  25. ssh.close()
  26. except paramiko.AuthenticationException:
  27. print("Authentication failed")
  28. except socket.gaierror:
  29. print(f"无法连接到{hostname}。请验证服务器是否存在且处于运行状态。")

此代码陷入了无限循环。命令仍在运行中。

  1. [在这里输入图像描述](https://i.stack.imgur.com/SZUMI.png)
英文:

channel.recv didn't return prompt back even after command is completed. 'while True' loop didn't break.

I am using paramiko to login a server with user 'xyz' and then 'su -' using paramiko channel.

  1. ssh = paramiko.SSHClient()
  2. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  3. try:
  4. ssh.connect(hostname, username='xyz', password=list_pass(hostname, 'xyz'))
  5. command = 'puppet agent -t --no-use_cached_catalog ; echo COMMAND_COMPLETED'
  6. channel = ssh.invoke_shell()
  7. channel.send("su -\n")
  8. time.sleep(2)
  9. channel.send(list_pass(hostname, 'root') + "\n")
  10. time.sleep(2)
  11. channel.send(command + '\n')
  12. time.sleep(5)
  13. while True:
  14. if channel.recv:
  15. time.sleep(1)
  16. print(channel.recv(1024).decode())
  17. if "COMMAND_COMPLETED" in channel.recv(1024).decode():
  18. break
  19. else:
  20. continue
  21. channel.close()
  22. ssh.close()
  23. except paramiko.AuthenticationException:
  24. print("Authentication failed")
  25. except socket.gaierror:
  26. print(f"Can't connect to {hostname}. Verify if server exists and it's up.")

This code went to infinite loop. Command still running.

enter image description here

答案1

得分: 0

我尝试了下面的代码,它有效。

  1. channel.send(command + '\n')
  2. time.sleep(5)
  3. # 创建一个新的接收缓冲区
  4. receive_buffer = ""
  5. while not receive_buffer.endswith("# ") or receive_buffer.endswith("$ "):
  6. '''
  7. 'root' 用户提示以 '# ' 结尾。
  8. 'non-root' 用户提示以 '$ ' 结尾。
  9. '''
  10. receive_buffer += channel.recv(9999).decode()
  11. print(receive_buffer)
  12. channel.close()
  13. ssh.close()

请注意,我保留了代码中的注释以提供更多上下文。

英文:

I tried below code and it worked.

  1. channel.send(command + '\n')
  2. time.sleep(5)
  3. # Create a new receive buffer
  4. receive_buffer = ""
  5. while not receive_buffer.endswith("# ") or receive_buffer.endswith("$ "):
  6. '''
  7. 'root' user prompt ends with '# '.
  8. 'non-root' user prompt end with '$ '.
  9. '''
  10. receive_buffer += channel.recv(9999).decode()
  11. print(receive_buffer)
  12. channel.close()
  13. ssh.close()

huangapple
  • 本文由 发表于 2023年5月21日 00:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定