Hi Jeff,
thank you for answer. You are right, executing "su -c 'command'" is
working fine:)
This is not exactly what i needed, but it is enough for now.
I would like to clarify one thing. You mentioned that "su" spawns a new
shell. Is that a reason why "run" command gets stuck while executing
"su"? Or is that because no exit code is being returned?
Regards
Mateusz Glowinski
On 28.12.2018 23:49, Jeff Forcier wrote:
Hi Mateusz,
The problem with `su` is that it spawns a new interactive shell,
expecting a human user to continue interacting with that shell during
a single overall SSH session. Tools like Fabric spawn a new session
for every new `run()` or similar, as described in this FAQ:
http://www.fabfile.org/faq.html#my-cd-workon-export-etc-calls-don-t-seem-to-work
Going by my local `man su` it looks like `su` can take a `-c` argument
to run its subshell that way (same as directly running the user's
login shell with `-c`, which usually means "run this one interpreted
string and exit").
So at a basic level you should be able to solve this by replacing:
dss.run("su", watchers=[...])
dss.run("whoami")
with:
dss.run("su -c whoami", watchers=[...])
though you'd necessarily need to have ALL subsequent commands mixing
in the `su -c` and `watchers=` bits; possibly a good spot for a
subroutine or subclass method.
We've got a `prefix` context manager that works well for commands that
can be strung together with `&&`, but we don't yet have an equivalent
for this sort of use case. We'll probably get one eventually!
Best,
Jeff
On Fri, Dec 28, 2018 at 5:17 PM Mateusz <[email protected]
<mailto:[email protected]>> wrote:
Hi There,
I'm trying to use fabric library to control subprocesson on linux
device. 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. Also 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:
|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. Any
ideas how to solve that?
I'm using:
Python 3.7
Fabric 2.4.0
Thanks in advance
Mateusz Glowinski
_______________________________________________
Fab-user mailing list
[email protected] <mailto:[email protected]>
https://lists.nongnu.org/mailman/listinfo/fab-user
--
Jeff Forcier
Unix sysadmin; Python engineer
http://bitprophet.org
_______________________________________________
Fab-user mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/fab-user