0
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))

I am executing SUDO command to this python script, but is throwing error "sudo: sorry, you must have a tty to run sudo".

4
  • Have you tried ssh -t [email protected] sudo command /path/to/file, i.e. use -t switch? Commented May 15, 2020 at 9:09
  • yes, i tried with '-t' as well, but it throwing same error Commented May 15, 2020 at 9:22
  • Can you paste the output for: sudo cat /etc/sudoers | grep requiretty Commented May 15, 2020 at 9:31
  • Defaults requiretty # changed in order to be able to use sudo without a tty. See requiretty above. Defaults:consul !requiretty Commented May 15, 2020 at 9:37

2 Answers 2

1

Try to run your script using

sudo -S python {script_name} {args}.

It worked for me in some such cases.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Type this in terminal:

sudo visudo /etc/sudoers

  1. find this line:

Defaults requiretty

and comment out the line using '#' i.e. #Defaults requiretty.

  1. Save and close the file:

In vi/vim: type “:wq” – this means pressing : first, which activates the command mode, then typing wq and pressing Enter. This sequence will save the file and exit the editor.

In nano: press Ctrl+X, then y to confirm you want to save changes. Then press Enter without changing the default filename.

2 Comments

It Worked.. You are genius.. Is there any way to do this in comment line, as i should not able to change that file in production environment
Some Linux distributions have been known to have this as a default configuration. RedHat just recently removed this from Fedora and REHL. If error occurs in production environment then I suggest you post another question with details about you production environment so that question will be helpful for other people who are looking for solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.