Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There,
I've prepared a piece of code, which intention is to log as a root user.
I can't use "sudo" method because that is not existing on the system that i want to control and logging directly as "root" is impossible. My idea is to log as a "admin" user and then use "su" command to switch to "root" user.

Section below contains code that is almost working for me. Any ideas how to solve that? Thanks in advance:)

What I have tried:

My environment:
Python 3.7
Fabric 2.4.0

Code:
Python
from fabric import Connection
from invoke import Responder

sudopass = Responder(pattern=r'Password:', response='adminPassword\n')

with Connection('192.168.0.106', user="admin", port=22, connect_kwargs={"password": "admin"}) as dss:
    command = "uname -s"
    print("Response on {} is: {}".format(command, dss.run(command)))
    command = "whoami"
    print("Response on {} is: {}".format(command, dss.run(command)))
    command = "su"
    print("Response on {} is after executing su command: {}".format(command,
                                                                    dss.run(command, pty=True, watchers=[sudopass])))
    command = "whoami"
    print("Response on {} is: {}".format(command, dss.run(command)))
print("Script end")


Output:
Linux
Response on uname -s is: Command exited with status 0.
=== stdout ===
Linux

(no stderr)
dssadmin
Response on whoami is: Command exited with status 0.
=== stdout ===
dssadmin

(no stderr)
Password: 
/home/dssadmin # 


As you can see script got stuck after sending "su" command.
Posted
Updated 28-Dec-18 23:40pm
v2
Comments
Richard MacCutchan 29-Dec-18 4:10am    
You need to talk to the owner of the Linux system. It may be that there are restrictions on using su.
MateoGlowinski 29-Dec-18 5:36am    
No, there are not. It's linux version prepared by our company, which is not including sudo command
Member 14102755 17-Jan-19 3:27am    
ExcelR Offers Business Analytics / Data Scientist Course / Data Analytics Training & Data Science Course Training In Bangalore, With 100% Placement.<a href="https://www.excelr.com/business-analytics-training-in-bangalore/”>Data science certification in Bangalore
Member 14102755 18-Jan-19 2:14am    
ExcelR Offers Business Analytics / Data Scientist Course / Data Analytics Training & Data Science Course Training In Bangalore, With 100% Placement.<a href="https://www.excelr.com/business-analytics-training-in-bangalore/”>Data science training in Bangalore

1 solution

I've found solution:
Python
sudopass = Responder(pattern=r'Password:', response='admin__password\n')

def su(connection, command) -> int:
    return connection.run(f"su -c '{command}'", pty=True, watchers=[sudopass])


Python
with Connection('192.168.0.106', user="dssadmin", port=22, connect_kwargs={"password": "password"}) as ssh_connection:
    command = "uname -s"
    print("Response on {} is: {}".format(command, ssh_connection.run(command)))
    command = "whoami"
    print("Response on {} is: {}".format(command, ssh_connection.run(command)))
    command = "opkg update"
    print("Response on {} is after executing su command: {}".format(command, su(ssh_connection, command)))
   command = "opkg install rescue-utils"
    print("Response on {} is after executing su command: {}".format(command, su(ssh_connection, command)))
    command = "whoami"
    print("Response on {} is: {}".format(command, su(dss, command)))
print("Script end")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900