remote_session.run_cmd("sudo -s") command stuck/waiting not processing for the next steps
hitendra002 opened this issue · 6 comments
I have this simple jumpssh code, Here I am trying to list down the available file on a remote server, which can be accessible to root user only.
from jumpssh import SSHSession
gateway_session = SSHSession('10.0.xx.xx', 'autouser', password='pwd123').open()
remote_session = gateway_session.get_remote_session('10.0.xx.yy', username="autouser", password='pwd123')
print("Exit code is:", remote_session.get_exit_code('ls'))
exit_code, output = remote_session.run_cmd("sudo -s")
print(exit_code)
print(output)
print(remote_session.get_cmd_output('ls -lrt'))
print("Closing connection")
remote_session.close()
gateway_session.close()
Output:
Exit code is: 0
Traceback (most recent call last):
File "D:/PycharmProjects/Python3/ssh/qcserver.py", line 19, in
exit_code, output = remote_session.run_cmd("sudo -s")
File "C:\Users\phitendra\AppData\Local\Programs\Python\Python38\lib\site-packages\jumpssh\session.py", line 387, in run_cmd
raise exception.RunCmdError(exit_code=exit_code,
jumpssh.exception.RunCmdError: Command (sudo -s) returned exit status (-1), expected [0]: [root@EMS autoinstall]#
Process finished with exit code 1
And my code waiting here for "sudo -s" execution, and after some time thrown error, while using the MobaXterm I am able to login to root using "sudo -s" command by just type and hit enter.
Is anything I am doing wrong here? plz get me the solution
Running interactive shell with sudo -s
is not possible with jumpssh
.
Here you have 2 possibilities:
remote_session.run_cmd("sudo ls -lrt")
Or
remote_session.run_cmd("ls -lrt", username=“root”)
Just few comments on the rest of the code:
- You don’t need to explicitly call
open()
onSSHSession
object, it will be done automatically when running first command - Calling
gateway_session.close()
will automatically close any remote session using this gateway, so no need to close remote_session explicitly
@t-cas thanks for the suggestion, But it didn't work for me facing the same issue, I am unable to login to card with root priveledges.
process get in stuck at below line
exit_code, output = remote_session.run_cmd("sudo ssh autouserl@10.3.x.xx")
even i have tried with
exit_code, output = remote_session.run_cmd("sudo ssh autouser@10.3.x.xx**\n**")
After some times its got failed and printed as below
Traceback (most recent call last):
File "D:/PycharmProjects/Python3/ssh/qcserver.py", line 23, in
exit_code, output = remote_session.run_cmd("sudo ssh autouser@10.3.X.XX")
File "C:\Users\phitendra\AppData\Local\Programs\Python\Python38\lib\site-packages\jumpssh\session.py", line 387, in run_cmd
raise exception.RunCmdError(exit_code=exit_code,
jumpssh.exception.RunCmdError: Command (sudo ssh autouser@10.3.X.XX) returned exit status (-1), expected [0]: STOP!!
[autouser@POC1 ~]$
But I can see that above the last line ([autouser@POC1 ~]$) is exactly what I want to log in and it happens but unfortunately with an error.
Update on above, using continuous_output=True
I can see that at the output its logged into successfully into the card which I wanted, But the problem is instead of finishing execution it is getting stuck over there and the program is terminating without executing next line of code.
remote_session.run_cmd("sudo ssh autouser@10.3.x.xx", continuous_output=True)
continuous output printed as
[autouser@DTMGR~]$
Can anyone help here what should I do to make it execute successfully?
My code sample is as below
from jumpssh import SSHSession`
gateway_session = SSHSession('10.0.xx.xx', 'autouser', password='pwd123')
remote_session = gateway_session.get_remote_session('10.0.xx.yy', username="autouser", password='pwd123')
print("Exit code is:", remote_session.get_exit_code('ls'))
remote_session.run_cmd("sudo ssh autouser@10.3.x.xx", continuous_output=True)
print(remote_session.get_cmd_output('ls'))
print("Closing connection")
gateway_session.close()
You cannot ssh interactively with a run_cmd("sudo ssh autouser@10.3.x.xx", ...)
Either you get a remote session (if you have permissions):
another_remote_session = remote_session..get_remote_session('10.3.x.xx', username="autouser")
another_remote_session.get_cmd_output('ls')
Or you directly add your command after ssh (this should work in you case, according to last logs provided):
remote_session.get_cmd_output("sudo ssh autouser@10.3.x.xx ls")
thanks, the second option worked for me remote_session.get_cmd_output("sudo ssh autouser@10.3.x.xx ls")
But the problem is to run multiple commands on that card for the operation like find match file and transfer to local i.e its very tuff and challenging as I cannot get another session from a remote session
Thanks @t-cas :)
Indeed it will be more challenging for you as you cannot get the extra remote session on 10.3.x.xx
but not impossible ;-)
Glad I could help and good luck to you !