def execute_cli_locally(command, timeout=CLI_EXECUTION_TIMEOUT,
return_output_as_string=True)
try:
logger.info("Executing commands locally :\n%s", command)
ssh = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = ssh.communicate(command)
if ssh.returncode == 0:
stdout = stdout.strip()
if len(stdout) != 0:
logger.info("Stdout :\n%s", stdout)
return stdout
else:
logger.error("Local command execution failed. Error :\n%s" % stderr)
print_response_and_exit(STATUS_FAILED,
"Local commands [%s] execution failed. Error :\n%s" %
(command, stderr))
我正在对此Python脚本执行SUDO命令,但抛出错误“ sudo:抱歉,您必须具有tty才能运行sudo”。
并使用'#'注释掉该行,即#Defaults requiretty。
在nano中:按Ctrl + X,然后按y确认要保存更改。然后按Enter键而不更改默认文件名。
sudo -S python {script_name} {args}
。