How to Fix \'Command Not Found\' Error in Python Paramiko exec_command?

DDD
Release: 2024-10-21 06:58:30
Original
832 people have browsed it

How to Fix

Unix Commands Failing with "Command Not Found" in Python Paramiko exec_command

When executing Unix commands using Python's Paramiko exec_command, it's possible to encounter an error such as " not found." This issue arises specifically when using the sesu command, while other commands like ls execute successfully.

Underlying Cause

The cause lies in the default behavior of SSHClient.exec_command, which doesn't run a "login" shell and doesn't allocate a pseudo terminal for the session. As a result, different startup scripts may run and specific environment variables might not be set.

Possible Solutions

To resolve this, several approaches can be taken:

  1. Utilize a Full Command Path: Use the complete path to the sesu executable, ensuring the command is independent of the environment.
  2. Adjust Startup Scripts: Modify startup scripts to consistently set PATH for both interactive and non-interactive sessions.
  3. Explicitly Invoke Login Shell: Run the script using --login with compatible shells, such as:
bash --login -c "sesu test"
Copy after login
  1. Modify Command Environment: Alter the command itself to include specific environment settings:
PATH="$PATH;/path/to/sesu" && sesu test
Copy after login
  1. Force Pseudo Terminal Use: (Not recommended) Enable pseudo terminal allocation by setting get_pty to True:
stdin,stdout,stderr = ssh.exec_command('sesu test', get_pty=True)
Copy after login

Additional Considerations

  • Verify LD_LIBRARY_PATH and ensure it accurately locates shared objects.
  • Refer to these resources for further insights:

    • [Environment Variable Differences When Using Paramiko](https://stackoverflow.com/questions/13781377/environment-variable-differences-when-using-paramiko)
    • [Certain Unix Commands Fail with "... Not Found" in JSch](https://stackoverflow.com/questions/44950115/certain-unix-commands-fail-with-not-found-when-executed-through-java-using-jsch)

The above is the detailed content of How to Fix \'Command Not Found\' Error in Python Paramiko exec_command?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template