Subprocess Hexdump Command Returning Bad Format Error - python-3.x

I am utilizing subprocess in order to grab the hexdump for a .tgz file as I require the hex string. The only problem is, hexdump is throwing a bad format error, but only when the command is issues through subprocess. I believe I have escaped everything correctly, but I can't figure out why I am not getting my intended output:
def package_plugin():
plugin_hex = subprocess.run(["hexdump", "-v", "-e", "'1/1 \"\\\\x%%02x\"'", "package.tgz"])
This results in an error: hexdump: "'1/1 "''x%%02x"'": bad format. However, if I just run the command straight in the terminal I receive the expected output of a hexstring with the '\x' separating the hex.
How should I be running this to store the output in a Python variable? Is my command being mangled somehow and hence not executing correctly? Any advice is appreciated.
Thanks
EDIT: I should add that when entering in the terminal the command is hexdump -v -e '1/1 "\\x%02x"' I am not sure why the extra '%' sign is shown in the error as it should be interpreting as a single % sign.

Nevermind. I couldn't figure this out but I solved it with hexlify:
def package_plugin():
plugin_hex = ""
with open('plugin.tgz', 'rb') as f:
for chunk in iter(lambda: f.read(32), b''):
plugin_hex += binascii.hexlify(chunk).decode("utf-8")
formatted_hex = '\\x' + '\\x'.join(plugin_hex[i:i+2] for i in range(0, len(plugin_hex), 2))

Related

os.system(cmd) call fails with redirection operator

My Python 3.7.1 script generates a fasta file called
pRNA.sites.fasta
Within the same script, I call following system command:
cmd = "weblogo -A DNA < pRNA.sites.fasta > OUT.eps"
os.system(cmd)
print(cmd) #for debugging
I am getting the following error message and debugging message on the command line.
Error: Please provide a multiple sequence alignment
weblogo -A DNA < pRNA.sites.fasta > OUT.eps
"OUT.eps" file is generated but it's emtpy. On the other hand, if I run the following 'weblogo' command from the command line, It works just find. I get proper OUT.eps file.
$ weblogo -A DNA<pRNA.sites.fasta>OUT.eps
I am guessing my syntax for os.system call is wrong. Can you tell me what is wrong with it? Thanks.
Never mind. It turned out to be that I was not closing my file, "pRNA.sites.fasta" before I make system call that uses this file.

Linux - Redirection of a shell script into a text file

I'm new to Linux, and have been trying to solve an assignment but to no avail.
I have a shell script which prints out lines of a text file in a certain manner (a line within every few seconds):
python << END
import time,random
a= open ('/home/ch/pshety/course/fielding_history.txt','r')
flag =False
for i in range(1000):
b=a.readline()
if i==402 or flag:
print(a.readline())
flag=True
time.sleep(2)
END
sh th.sh
If I run it without trying to redirect it anywhere, I get the output on the terminal. However, when I tried to redirect it into a new text file, it doesn't do anything - the text remains empty:
sh th.sh > debug.txt
I've tried looking for answers, I've stumbled upon a lot of suggestions including tee but nothing helps - the file remains empty.
What am I doing wrong?
Try this:
import time,random
a = open('/home/ch/pshety/course/fielding_history.txt', 'r')
for i in range(1000):
b = a.readline()
if i >= 402:
print(b, flush=True)
time.sleep(2)
Your Python script likely needs to flush the contents of the output buffer before you can see it.
Note: aside from the sleep() call, Unix provides other ways of accomplishing this. I would take a look at man tail and read about the -f and -n switches.
Edit: didn't realize that tail has a switch (-s) to sleep as well!

Why is stdout printing out an empty string in this case?

I am reading about subprocess and playing around with some code.
I'm using Windows 7 with Python3.6
import subprocess
process = subprocess.Popen(['notepad', 'C:\\Users\Amit\Downloads\InsiderTrades.txt'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#I'm opening a text file which has a list of stock tickers
stdout1, stderr1 = process.communicate()
print(stdout1.decode('ASCII'))
The output I'm getting is nothing or
b'' as the value for stdout1.
I"m not quite sure what communicate is outputting in this case.
I was under the impression that it would output the text from my text file or it would output anything I type into the text file.
I tried typing into the newly opened text file as well, but I'm still getting the same output , b''
So what am I getting an empty string, despite typing something into the newly opened textfile.
Subprocess is basically as if you run that command in the terminal.
So what you are doing is running
notepad some_file.txt
which just opens a file in notepad, but it doesn't send anything to standard output.
If you run a command that writes something to standard output, then you will have a non-empty stdout1. Try ls for example if you are on a *nix system or dir if on Windows.

How to call from within Python an application with double quotes around an argument using subprocess?

I'm trying to call certutil from inside python. However I need to use quotation marks and have been unable to do so. My code is as follows:
import subprocess
output= subprocess.Popen(("certutil.exe", "-view", '-restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"' ), stdout=subprocess.PIPE).stdout
for line in output:
print(line)
output.close()
I thought the single quotes would allow me to use double quotes inside the string.
Also I have tried using double quotes with the escape character (\"), however I keep getting the same error:
Unknown arg: -restrict \\NotAfter\r\n'
For some reason it seems to be translating " into \\.
Can anyone give insight as to why and how to fix it?
I do not have Python in any version installed. But according to answer on How do I used 2 quotes in os.system? PYTHON and documentation of subprocess, subprocess handles requirement for double quotes on arguments with space automatically.
So you should need to use simply:
import subprocess
output= subprocess.Popen(("certutil.exe", "-view", "-restrict", "NotAfter <= now+30:00, NotAfter >= now+00:00" ), stdout=subprocess.PIPE).stdout
for line in output:
print(line)
output.close()
And the command line used on calling certutil is:
certutil.exe -view -restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"
output=subprocess.Popen(("certutil.exe -view -restrict \"NotAfter<=now+30:00,NotAfter>=now+00:00\"" ),stdout=subprocess.PIPE).stdout
This is what was needed. I was passing the command as a command with 2 args. What I should have been doing was passing it as one big command with 2 parameters.

Can't read to var in Bash

I wrote a little Bash script and I'm having a problem while reading from the command line. I think its because I wrote the script on Windows. Here is the code:
read NEW_MODX_PROJECT
and the output of the debug mode
+ read $'NEW_MODX_PROJECT\r'
Finally here the error I get
': Ist kein gültiger Bezeichner.DX_PROJECT
I think in English it should mean "': is not a valid identifier.DX_PROJECT"
While writing it on Windows, it worked fine. I used console2 to test it which is using the sh.exe.
Your assertion is correct -- Windows uses CRLF line separators but Linux just uses a LF.
The reason for your strange error message is that while printing the name of your variable, it includes the carriage return as part of its name -- the terminal then jumps back to the first column to print the rest of the error message (which overwrites the beginning of the message with the end of it).
There are a set of utilities known as dos2unix and unix2dos which you can use to easily convert between formats, e.g.:
dos2unix myscript.sh
If you don't happen to have them, you can achieve the same using tr:
tr -d '\r' < myscript.sh > myscript-new.sh
Either will strip all the carriage returns and should un-confuse things.

Resources