Home >Backend Development >Python Tutorial >How to Eliminate Junk Values in Paramiko SSH Output?
Ridding Junk Values from SSH Output using Paramiko
When fetching output from a remote machine's CLI via Paramiko's SSH library, one may encounter unsolicited characters such as "x1b[2Jx1b[1;1H" and "u." These are ANSI escape codes that embellish output for terminal clients.
Cause and Resolution
Paramiko's SSHClient.invoke_shell prompts for a pseudo terminal, resulting in the appearance of these escape codes. For automated command execution, it's recommended to use SSHClient.exec_command instead, which does not allocate the pseudo terminal by default.
<code class="python">stdin, stdout, stderr = client.exec_command('ls')</code>
Alternative Solutions
If using the "shell" channel is imperative, it's feasible to do so without the pseudo terminal, but Paramiko's SSHClient.invoke_shell does not offer this feature. One can manually create the "shell" channel instead.
<code class="python"># Not supported by Paramiko SSHClient.invoke_shell channel = ssh_client.get_transport().open_channel("session") channel.exec_command("ls")</code>
Unicode Encoding Note
The "u" prefix in the output strings indicates Unicode encoding, which should be preserved.
The above is the detailed content of How to Eliminate Junk Values in Paramiko SSH Output?. For more information, please follow other related articles on the PHP Chinese website!