I have to implement a scenario where I need to login as root and then perform some ssh and scp commands through python to automate certain workflows.
Example: Suppose MACHINE12 is my host and current user is test, i.e test@MACHINE12
I want a interactive code to be written in such a way that , if I put the following commands it should login as root user and the session should be maintained to perform other operations in python.
{test@MACHINE12} su
password:
once password is entered, logged in as root.
{test@MACHINE12 test}
Perform other commands here
{test@MACHINE12 test} ssh user1@abc
I tried this using the pexpect command in python, but the root session is not maintained or staying to perform other operations.
The commands can be only executed if the logged in session is root because abc host need root permission.
Find my tried snippet here:
def loginAsroot():
childTab = pexpect.spawn("su") //command su to enter to root
childTab.expect(":")
childTab.sendline("asdgfgh") // entering password
childTab.expect("#")
def runCommands():
command = "ssh user1@abc 'cat /dfg/hello.txt'>> /doc/g/auth.txt"
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate(timeout=10000)
return output
//function call
loginAsroot()
runCommands()
I'am trying to do linux command execution it is giving permission denied error, because the root session is not staying, its getting logged out. Hence Permission denied
How can I achieve this kind of executions?