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

DDD
Release: 2024-10-21 06:57:02
Original
433 people have browsed it

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
Copy after login
  • Configure startup scripts to consistently set the PATH:
  • Run the command explicitly via a login shell:
bash --login -c "sesu test"
Copy after login
  • Alter the environment within the command itself:
PATH="$PATH;/path/to/sesu" && sesu test
Copy after login
  • As a last resort (not recommended):
stdin,stdout,stderr = ssh.exec_command('sesu test', get_pty=True)
Copy after login

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!

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