exscript
exscript copied to clipboard
Generic method to run a remote sudo command
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?