exec sudo commands on ssh in python - python

I need a python lib to execute ssh command. I also need the output.
I tried paramiko: It was exactly what i needed but no way to execute sudo commands there. there are some online posts for that but none seem to work.
I also tried fabric: The problem is there is no way to capture output also sometimes it shows error while setting env.
Can anybody suggest something. A example of exec some sudo cmd over ssh will be good enough.

Fabric's operation.run captures stdout and also stderr if you pass combine_stderr to run(). See http://docs.fabfile.org/en/1.3.4/api/core/operations.html#fabric.operations.run

Related

Paramiko return code127 against command that actually exist

During automating SSH routine using paramiko to linux pc, I am encountering an issue where for the existing command, I am not able to execute it successfully as "command not found" 127 response code is received on command execution.
I have tried all sort of ways exec_command(), crosschecking manually (the command work fine), change directory to the path of command before execution, still no luck.
However, the same command works well with Java. Any thoughts on it.
Not sure what your real problem is but you can use full pathname to the command which is supposed to always work. Like
exec_command('/bin/ls')

running as sudo from python for pyshark / tshark

I'm trying to do a live capture with pyshark, but it wants to run tshark using sudo. I'm not sure how to run sudo out of python. The github thread states: "you can create a 'script' that just runs "sudo tshark" and tell pyshark to run that instead of tshark."
Buuuuut I'm not too sure how to do that. I was looking at Using sudo with Python script
but again not sure how to "run that instead of tshark"
Has anyone done this? Can anyone advise?
Bit more info here: If you're an admin user, you don' t need sudo to run "tshark -c 100 -i en0". If you "sudo chmod 777 /dev/bpf*" that works for things like Carnivore in Processing, but does zip all for Pyshark. Trying to edit Startup items to give you read access is moot on OSX because Yosemite tossed it.
Other info: https://apple.stackexchange.com/questions/138694/what-is-access-bpf-group
I'm really starting to think something is just up w/ PyShark itself.
Thanks
Don't use sudo to run Wireshark. Instead, configure your user account to be able to use Wireshark without root access. Detailed instructions are here: https://ask.wireshark.org/questions/7976/wireshark-setup-linux-for-nonroot-user
WELP. turns out it was just because I hadn't used 'en0' Marking this as solved. HA.

Paramiko error when trying to edit file: "sudo: no tty present and no askpass program specified"

I am using Paramiko to SSH and edit a config file. The file itself needs sudo permissions to edit. This hasn't been a problem so far, as I've just done echo <sudopw> | sudo <command> for other sudo commands in my script.
When I try to edit this file using sed, though, nothing happens. stderr produces: sudo: no tty present and no askpass program specified
Here is my code:
stdin, stdout, stderr = client.exec_command
('echo <sudopassword> | sudo sed -i -e \"\\$aAllowUsers\" /etc/ssh/sshd_config)')
I have tried solutions using invoke_shell but nothing seems to be working. Any solution to edit this file would be helpful.
EDIT: This has been solved! Don't use get_pty. Use the -S option of sudo right after "sudo".
If you read the error message
sudo: no tty present and no askpass program specified
then you can easily find the solution: add the -t option to your ssh command:
-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
This has been discussed before:
How to fix 'sudo: no tty present and no askpass program specified' error?
sudoers NOPASSWD: sudo: no tty present and no askpass program specified
Regarding Paramiko, there have been related questions, with a couple of different approaches:
use the get_pty method of the ssh Channel to obtain a pseudo-terminal (which is analogous to telling ssh to do this)
use the -S option of sudo, and send the password on your standard output.
For discussion, see the suggested answers here:
Paramiko and Pseudo-tty Allocation
Nested SSH session with Paramiko

Python making cmd invisible when using the os.system

I found out that i could ping a system on python by typing
os.system('ping ip')
but when i execute it, it shows cmd.
My question is, how do i ping someone on python without showing the cmd?
If you need only a ping, then it would be better to use something like ping.py.
In other cases use subprocess as suggested by #Sentinel
Look at the
http://docs.python.org/library/subprocess.html
module.
It gives you enough options for controlling the output.
Or use standard bash redirection in order to send the output to /dev/null
See my answer to hiding console when run in os.startfile()

Missing 'read' prompt in bash when using ssh?

Please tell me I'm missing something really obvious here:
$ cat ~/bashplay/f
#!/bin/bash
read -p 'RDY> ' x
echo $x
$ ~/bashplay/f
RDY> direct execution
direct execution
$ ssh somehost ~/bashplay/f
indirect via ssh
indirect via ssh
Note the missing "RDY>" prompt when using ssh. I see the same thing in python when using the "readline" package. Anyone know why?
From man bash:
-p prompt
Display prompt on standard error, without a trailing new‐
line, before attempting to read any input. The prompt is
displayed only if input is coming from a terminal.
Use the ssh option -t which forces pseudo tty allocation:
ssh -t somehost ~/bashplay/f

Categories