Fabric - Run method is not being finished - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Fabric - Run method is not being finished (/thread-14992.html) |
Fabric - Run method is not being finished - mglowinski93 - Dec-28-2018 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. Code: 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: As you can see script got stuck after sending "su" command. Any ideas how to solve that? Thanks for help in advance:)=========================================================================== Python 3.7 Fabric 2.4.0 RE: Fabric - Run method is not being finished - nilamo - Dec-28-2018 Looks like it's maybe still waiting for the password? Maybe try changing the pattern to include trailing whitespace: r"Password:\s*?"
RE: Fabric - Run method is not being finished - mglowinski93 - Dec-28-2018 No no, it was logged properly for sure. I can see that after sending "su" command it is now in /home/dssadmin directory as it should be after successful loggin. Sorry, i should mention that in my post:) According to me it's waiting for some exit code or sometthing RE: Fabric - Run method is not being finished - mglowinski93 - Dec-29-2018 So far i've this: So far i've this: 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]) 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")Not exactly what i needed but working fine |