79209508

Date: 2024-11-21 02:48:54
Score: 0.5
Natty:
Report link

When obtaining the channel, set a separate pty so that EOF can be immediately obtained when the command ends. Otherwise, it is uncertain whether other processes in the system might affect stdout.

client.connect(hostname='127.0.0.1', port=2222, username='root', password='xxx',
               timeout=10, banner_timeout=300)
cmd = "echo $PATH\n"
stdin, stdout, stderr = client.exec_command(cmd, get_pty=True)
stdout.readlines()

or

client.connect(hostname='127.0.0.1', port=2222, username='root', password='xxx',
               timeout=10, banner_timeout=300)
channel = client.get_transport().open_session(timeout=10)
channel.get_pty()
channel.settimeout(10)
cmd = f'echo $PATH\n '
channel.exec_command(cmd)
stdin = channel.makefile('wb')
stdout = channel.makefile('r')
stdout.readlines()
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When
  • Low reputation (1):
Posted by: user28406068