starting tshark with python - invalid command - python

I'm trying to start tshark with python. Testing the command in command line seems to be OK. but while starting it with python, I'm getting error as described below.
Any idea what is going wrong?
cmd1='"tshark -i 1 -Y "ssdp" -T fields -E separator=, -E header=y -e ip.id -e ip.checksum"'
cmd=shlex.split(cmd1)
try:
tsharkProc = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
executable="C:\\Program Files\\Wireshark\\tshark.exe")
except:
print("ERROR {} while running {}".format(sys.exc_info()[1], cmd))
And the error:
Capturing on 'Ethernet 2' tshark: Invalid capture filter "/c tshark -i
1 -Y ssdp -T fields -E separator=, -E header=y -e ip.id -e
ip.checksum" for interface 'Ethernet 2'.
That string isn't a valid capture filter (can't parse filter
expression: syntax error). See the User's Guide for a description of
the capture filter syntax. 0 packets captured

Related

Forming a cURL command using string concatenation in Python

I'm trying to form a cURL using string concatenation in Python. I have a custom command and I'm not sure if I can use requests for this.
cmd='curl -u admin:'+password+' -F file=#'+package+' -F name='+package+' -F force=false -F install=true http://'+ip+':5101/crx/packmgr/service.jsp'
print(cmd)
Error:
curl -u admin:******** -F file=#abc_xyz.zip
-F name=abc_xyz.zip
-F force=false -F install=true http://8.8.8.8:5101/crx/packmgr/service.jsp
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
sh: line 1: -F: command not found
sh: line 2: -F: command not found
32512
And yes I have tried escape '' as well. Didn't work.
Any help would be greatly appreciated. TIA
Looks like your constructed command has line endings after the package variable, try stripping it from extra symbols:
package = package.strip()

Running subprocess with spaces in options in python

I tried to search for an answer for a while, but I did not find anything so far for my specific case. I want to run command in python:
ssh -o ConnectTimeout=3 -o ProxyCommand="ssh -q -W %h:%p bastion.host.com" host.com "screen -dmS TEST /bin/bash --login -c 'yes | script.sh --option-1 value1 -option2 value2 2>&1 | tee output.log'"
this is my code:
import subprocess
server_command = "screen -dmS TEST /bin/bash --login -c 'yes | script.sh --option-1 value1 -option2 value2 2>&1 | tee output.log'"
command = ['ssh', '-o', 'ConnectTimeout=3', 'ProxyCommand="ssh -q -W %h:%p bastion.host.com"', 'host.com', server_command]
p = subprocess.Popen(command, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
stdout, stderr = p.communicate(input=None)
Everything was working (screen was spawned with script running) until I added option with spaces: ProxyCommand="ssh -q -W %h:%p bastion.host.com".
After that I get error:
>>> print(stderr)
b'ssh: Could not resolve hostname ProxyCommand="ssh -q -W %h:%p bastion.host.com": Name or service not known\r\n'
How can I please pass this option to my command?
Your SSH command contains invalid arguments: ProxyCommand is an option, so it needs to be preceded by -o, same as ConnectTimeout (and, as noted by Charles Duffy, the redundant quotes inside that option string need to be removed, since the command is not passed to the shell):
server_command = 'screen -dmS TEST /bin/bash --login -c \'yes | script.sh --option-1 value1 -option2 value2 2>&1 | tee output.log\''
command = ['ssh', '-o', 'ConnectTimeout=3', '-o', 'ProxyCommand=ssh -q -W %h:%p bastion.host.com', 'host.com', server_command]
In general when your command line contains spaces and/or quotes and is passed to another command, it may be necessary to shell-quote it. The Python function shlex.quote automates this. In your case it’s not necessary because you (correctly) manually quoted the command you’re passing to screen inside server_command. Alternatively you could have written the following:
script_command = 'yes | script.sh --option-1 value1 -option2 value2 2>&1 | tee output.log'
server_command = f'screen -dmS TEST /bin/bash --login -c {shlex.quote(script_command)}'
— Note the absence of manual quotes inside the shell command line. The advantage over manual quoting is that this will also work with nested levels of shell quoting, e.g. when nesting command invocations.

Docker run inside a container

I have a Python method code using docker and I try to understand it. The method is here,
def exec(self, container_target, command, additional_options=""):
""" execte docker exec commmand and return the stdout or None when error"""
cmd = """docker exec -i "%s" sh -c '%s' %s""" % (
container_target, command, additional_options)
if self.verbose:
print(cmd)
try:
cp = subprocess.run(cmd,
shell=True,
check=True,
stdout=subprocess.PIPE)
return cp.stdout.decode("utf-8").strip()
except Exception as e:
print(f"Docker exec failed command {e}")
return None
I get the screenshot at the time of debugging,
The cmd value is found,
'docker exec -i "craft_p2-2" sh -c \'cd craft && composer show
--name-only | grep nerds-and-company/schematic | wc -l\' '
My understanding is the code using the shell of the container named craft_p2-2 and enters a folder named craft. Then, it checks if the Schematic plugin is installed. Is that correct?
This might be obvious for some, but, I don't come with a wealth of container knowledge and need to be sure of what's going on.

Popen can find the existing tool

I'm trying to run the following;
def conn(ad_group):
result = Popen(["sudo -S /opt/quest/bin/vastool", "-u host/ attrs 'AD_GROUP_NAME' | grep member"], stdout=PIPE)
return result.stdout
on a RedHat machine in a python script but I'm getting FileNotFoundError: [Errno 2] No such file or directory: 'sudo -S /opt/quest/bin/vastool'
I can run the command(sudo -S /opt/quest/bin/vastool -u host/ attrs 'AD_GROUP_NAME' | grep member) at the command line without a problem.
I'm sure I've messed up something in the function but I need an other set of eyes.
Thank you
You need to make the entire command a single string, and use the shell=True option because you're using a shell pipeline.
result = Popen("sudo -S /opt/quest/bin/vastool -u host/ attrs 'AD_GROUP_NAME' | grep member", stdout=PIPE, shell=True)

Run two ffmpeg commands one after another

I need to run two ffmpeg commands, one after the other i.e., wait until the first command has finished, and then run the second command. The first command is
ffmpeg -threads 8 -i D:\imagesequence\dpx\brn_055.%04d.dpx D:\imagesequence\dpx\test2.mov
and the second is
ffmpeg -i D:/imagesequence/background.jpg -vf "movie='D\:/imagesequence/dpx/thumbnail.jpg' [watermark]; [in][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/3 [out]" D:/imagesequence/dpx/final_with_text_mod_04.jpg
What is the best way to accomplish this in Python?
You don't have to do anything more than calling 2 times a ffmpeg command with subprocess python module, this is already the default behaviour
import subprocess
execstr1 = 'ffmpeg -x -y -z ...'
execstr2 = 'ffmpeg -a -b -c ...'
out1 = subprocess.check_output(execstr1, shell=True)
out2 = subprocess.check_output(execstr2, shell=True)

Categories