Home  >  Article  >  Backend Development  >  Why Do Some Unix Commands Return \"not found\" Error in Python\'s Paramiko Exec_Command?

Why Do Some Unix Commands Return \"not found\" Error in Python\'s Paramiko Exec_Command?

DDD
DDDOriginal
2024-10-21 06:57:02357browse

Why Do Some Unix Commands Return

"not found" Error with Unix Commands in Python's Paramiko

When attempting to execute the command 'sesu' on a Unix server using Python's Paramiko exec_command, users may encounter the error message "sh: sesu: not found." This issue arises only with certain commands, such as 'sesu,' while other commands, like 'ls,' function as intended.

This error stems from a default behavior in SSHClient.exec_command, which does not run the shell in "login" mode and does not allocate a pseudo terminal. Consequently, the command execution may differ from an interactive SSH session, where certain startup scripts and environment variables are sourced or used.

Possible Solutions

The following solutions are recommended in order of preference:

  • Modify the command to specify the full path to the executable:
/bin/sesu test
  • Configure startup scripts to consistently set the PATH:
  • Run the command explicitly via a login shell:
bash --login -c "sesu test"
  • Alter the environment within the command itself:
PATH="$PATH;/path/to/sesu" && sesu test
  • As a last resort (not recommended):
stdin,stdout,stderr = ssh.exec_command('sesu test', get_pty=True)

The above is the detailed content of Why Do Some Unix Commands Return \"not found\" Error in Python\'s Paramiko Exec_Command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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