Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic method to run a remote sudo command #216

Open
mpennington-te opened this issue Oct 22, 2021 · 0 comments
Open

Generic method to run a remote sudo command #216

mpennington-te opened this issue Oct 22, 2021 · 0 comments

Comments

@mpennington-te
Copy link

I have been trying to figure out how to manually run a sudo command with Exscript, but the way to do so escapes me. I'm trying to use send() to send the sudo command, but I can't seem to detect the password prompt after I do so.

I think send() may be the wrong way to invoke a sudo, but I can't find another way when I use SSH2().

There's probably some exscript automation that could handle the sudo in a "black box" manner, but I'd like to have full control over what happens when I try to sudo... so I'd prefer to do something along the lines of manual send(), expect(), etc...

I'm running exscript 2.6.3 / Python 3.8.12 / macOS Catalina...

from getpass import getpass
import os

from Exscript.protocols import SSH2
from Exscript import Account

MY_USERNAME = "nobody"

def login_linux(password, host, command):
    account = Account(MY_USERNAME, password)
    conn = SSH2(driver='generic', debug=0)
    conn.connect(host)
    conn.login(account)

    sudo_check_linux(conn, command)

def sudo_check_linux(conn, command):

    conn.send("sudo ls\r")
    conn.expect("assword: ")  # <---- I can never detect the expect prompt
    conn.execute(password)

    conn.execute(command)
    output = conn.response

    try:
        conn.send('exit\r')
        conn.close()
    except:
        pass

    return output

if __name__=='__main__':
    password = getpass("password for %s: " % MY_USERNAME)
    host = "this_host.localdomain"
    output = login_linux(password, host, "sudo uname -a")
    print("%s\n%s" % (host, output))

Could someone give me a pointer on how to fix this script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant