Pipe character is messing up my pexpect command string - python

I have a string that I build then pass to pexpect:
command = "for i in `python /tmp/"+ cecUser + "_getSyslogs.py " + startString + " " + endString + " " + myDir + "`;do gzcat /emslogs/archive/" + myDir + "/$i | grep " + node + " | egrep -v \"" + filteredWords + "\" >> /tmp/" + syslogFile + ";done"
p.sendline(command)
If filteredWords = "No LMA address", the script runs fine.
If filteredWords = "No LMA address|CLISess", the script fails.
What am I missing? I tried to escape the | by preceding with \ and that didn't help. I actually tried a whole slew of different combinations and the script still fails. What is even odder is the logging output from p.logfile looks fine:
$ for i in `python /tmp/getSyslogs.py 20141227120000 20141228120000 local1`;do gzcat /emslogs/archive/local1/$i | grep ALPRGAGQPN2 | egrep -v "No LMA address|CLISess" >> /tmp/1419900585.83_ALPRGAGQPN2_syslogs.txt;done
If I cut'n'paste that line on the target machine, it works no problem. There is just something about the pipe in the egrep that is messing up when passed to p.sendline.

Related

python subprocess.run() wrongly parses command

I use python to execute the shell commands. The command call make with some extra flags:
version_info = r"CXXOPTS+='-DVERSION=" + r'\"' + args.version + r'\"' + r"'" + \
" -DSOFTWARE_FLAVOR=" + r'\"' + args.software_flavor + r'\"' + \
" -DSOFTWARE_DECORATOR=" + r'\"' + args.software_decorator + r'\"' + r"'"
where args is the arguments parsed by ArgumentParser from the command line that called this python script.
Then I execute:
command = f"make {version_info} all"
subprocess.run(command, shell=True, encoding="iso-8859-1", check=True)
It reports error: unknown output-sync type FTWARE_FLAVOR.
It seems it cannot recognize -DSOFTWARE_FLAVOR.
Then I tried to split this command line as follows:
version = r"CXXOPTS+='-DVERSION=" + r'\"' + args.version + r'\"' + r"'"
flavor = r"CXXOPTS+='-DSOFTWARE_FLAVOR=" + r'\"' + args.software_flavor + r'\"' + r"'"
.....
Then it works correctly.
What is the reason of this error?
Thank you in advance.
I think your life becomes simpler if you drop the shell=True and pass a list to subprocess.run, because this allows you to drop much of the elaborate quoting you're using to create your command line. For example:
from collections import namedtuple
version_info = (
f'CXXOPTS+=-DVERSION="{args.version}" '
f'-DSOFTWARE_FLAVOR="{args.software_flavor}" '
f'-DSOFTWARE_DECORATOR="{args.software_decorator}"'
)
command=['make', version_info, 'all']
subprocess.run(command)
If I have a Makefile that looks like this:
all:
echo CXXOPTS: "$(CXXOPTS)"
And I add some code to populate args:
import argparse
import subprocess
p = argparse.ArgumentParser()
p.add_argument('--version', default='1')
p.add_argument('--software-flavor', default='vanilla')
p.add_argument('--software-decorator', default='martha.stewaret')
args = p.parse_args()
version_info = (
f'CXXOPTS+=-DVERSION="{args.version}" -DSOFTWARE_FLAVOR="{args.software_flavor}" '
f'-DSOFTWARE_DECORATOR="{args.software_decorator}"'
)
command=['make', version_info, 'all']
subprocess.run(command)
Then I get as output:
echo CXXOPTS: -DVERSION="1" -DSOFTWARE_FLAVOR="vanilla" -DSOFTWARE_DECORATOR="martha.stewaret"
CXXOPTS: -DVERSION=1 -DSOFTWARE_FLAVOR=vanilla -DSOFTWARE_DECORATOR=martha.stewaret

FFMPEG File Output is Still in Use By a Process

I am trying to complete this part of my program. In this section, I am trying to speed up or slow down a video based on a factor variable. Once it's done, I use moviepy to turn it into a VideoFileClip, then I delete the file.
if factor <= 2:
system("ffmpeg -i " + paths[dex] + " -vf setpts=" + str(vfactor) + "*PTS -an ./Media/Videos/temp.mp4")
system("ffmpeg -i " + paths[dex] + " -filter:a atempo=" + str(factor) + " -vn ./Media/ShortSounds/temp.mp3")
elif 2 < factor < 4:
factor = round(sqrt(factor), 1)
system("ffmpeg -i " + paths[dex] + " -vf setpts=" + str(vfactor) + "*PTS,setpts=" + str(vfactor) + "*PTS -an ./Media/Videos/temp.mp4")
system("ffmpeg -i " + paths[dex] + " -filter:a atempo=" + str(factor) + ",atempo=" + str(factor) + " -vn ./Media/ShortSounds/temp.mp3")
elif factor > 4:
raise Exception("File " + paths[dex] + " is too long.")
t = VideoFileClip("./Media/Videos/temp.mp4")
t.audio = AudioFileClip("./Media/Videos/temp.mp3")
templist.append(t)
remove("./Media/Videos/temp.mp4")
However, when the code gets to the deletion command, it has the following error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: './Media/Videos/temp.mp4'
What's strange is, I can see the temp.mp4 file, and it runs just fine. I never get this error while manually running the temp.mp4 file.
I have tried the following:
Waiting 5, 10, and 20 seconds before deleting the file.
Running "taskkill -f -im ffmpeg.exe" before deleting the file
I went through the debugger, and right before the deletion, I checked in task manager to see if ffmpeg was still running, and it wasn't.
Do you guys have any idea what could be holding this up? My code worked previously when I was trying to just do audio, but I am trying it with video and this is happening.

python function returning "22, 'Invalid argument'" when calling from jenkins framework

I am using jenkins framework to run python script in unix shell but its giving error like :
CRITICAL - (22, 'Invalid argument')
Same script works fine when execute manually from shell.
Here is my code:
FUNCTION 1:
def check_diskspace(ip,usr):
l = []
cmds='ssh %s#%s'%(usr,ip)
child1=pexpect.spawn(cmds)
for i in range(1,5):
child1.sendline("df -h /d/oss/global | awk '{if (NR>2) {print}}' | awk -F ' ' '{print $" + str(i) + "}'")
child1.expect(prompt)
l.append(child1.before.strip(cmds).split('\n')[1])
return l
FUNCTION 2
def TC1():
logger.info("Start TC1")
l = check_diskspace(nfsip,username)
Totalspace = l[0].strip('\r')
print "Totalspace=",Totalspace
if('global_100fill' == sys.argv[1]):
TC1()
Note: I am calling script from jenkins as:
/usr/local/bin/python2.7 /var/lib/jenkins/workspace/Stress_test/Stress_framework/usecase.py global_100fill

Python code to send command through command line

final="cacls " + "E:/" + "\"" + list1[2] + " " + list1[3] + "\"" + " /p " + str
os.system(final)
I am trying to set permission to a folder Using Python but while running this command , User input needs to be provided too i.e
it asks ARE YOU SURE(Y/N) and the user needs to enter "Y" or "N"
Is there any way to use python to send the user input "Y" along with the above code?
pro = subprocess.Popen(final,shell=True, stdin=subprocess.PIPE)
pro.communicate(bytes("Y\r\n",'utf-8'))
I have added the following code . The program exits without setting the permission.
http://jimmyg.org/blog/2009/working-with-python-subprocess.html#writing-to-standard-input
Try using the subprocess module
import subprocess
cmd = ["cacls", "E:/" + list1[2], list1[3], "/p", str]
pro = subprocess.Popen(final, stdin=subprocess.PIPE)
pro.communicate("y\r\n")
As a smart programmer, use PBS
Then, the code is:
from pbs import type as echo# Isn't it echo for Windows? If not, use the correct one
script = Command("/path/to/cacls ")
print script(echo("Y"), ("E:/" + "\"" + list1[2] + " " + list1[3] + "\"" + " /p " + str).split())

Usage of subprocess class in Python

final="cacls " + "E:/" + "\"" + list1[2] + " " + list1[3] + "\"" + " /p " + str
pro = subprocess.Popen(final,shell=True, stdin=subprocess.PIPE)
pro.communicate(bytes("Y\r\n",'utf-8'))
Trying to set permission to a folder Using Python but while running this command , User input needs to be provided too i.e
it asks ARE YOU SURE(Y/N) and the user needs to enter "Y" or "N"
The above code is not setting the permission.
This was the question i had asked before:
Python code to send command through command line
As a smart programmer, use PBS
Then, the code is:
from pbs import type as echo# Isn't it echo for Windows? If not, use the correct one
script = Command("/path/to/cacls ")
print script(echo("Y"), ("E:/" + "\"" + list1[2] + " " + list1[3] + "\"" + " /p " + str).split())

Categories

Resources