I have small script in bash which automates installation of a few packages. It installs a few packages using apt-get and the rest from sources. I have all the sources I need tarred into a single tar file. The sample script looks as follows:
#!/bin/bash
apt-get install wget gcc g++ ruby php5 php5-dev xz-utils bzip2 -y
cd /usr/local/bin/
wget my-sources.tar.gz
tar zxvf my-sources.tar.gz
cd my-sources
tar zxvf package1.tar.gz
cd package1
./configure && make && make install
cd /usr/local/src/my-sources
tar zxvf package2.tar.gz
cd package2
./configure && make && make install
cd /usr/local/src/my-sources
tar zxvf package3.tar.gz
cd package3
./configure && make && make install
echo -e "\nFinished\n"
This script is then encoded using zlib
in python 2.7:
import tempfile
import subprocess
import zlib
with open("/usr/local/src/sample.sh") as inputfile:
teststr = zlib.compress(inputfile.read()).encode('base64')
print teststr
This is then passed to a python script which executes it as follows:
thisstr = "Gu11nX1eVdeGlaMoouIyJtOV/cPBWrp1b7OeY7P7GXtzb
x7HETWRhTHC6NzM3k0nH6dw8uFs+qRtkJWiPrBGs1mlXWJjt7ZSUHe0
ZougLFsrAmxs3b+l+q9UKFrL1aAt0glTEo8bUuSO7Gjfe3JaYpedEgt
qkbZtz825OWUlyiz+pMPmkOdlhYu2ia+at+ZJIGZRkFzsBIqJKNhAQ4
LlBdMzdGu593UzCBtsspZiVntsFlzbyefpjCBk+PDKbyefOZMPy9Xd/
wL3ieojA=="
str=zlib.decompress(thisstr.decode('base64'))
with tempfile.NamedTemporaryFile() as scriptfile:
scriptfile.write(str)
scriptfile.flush()
subprocess.call(['/bin/bash', scriptfile.name])
What happens here is, while apt-get
is being executed, in-between, the wget
is attempted. Since wget
isn't installed yet, the wget command fails and then the execution moves to the compiling and building steps (which throw errors as the files aren't available). All the while, the apt-get
is still being executed.
I tried 2 steps to avoid this:
sleep 5
at the end of the apt-get
command. This did not help. apt-get
in another function and called the function just wget
command. this too did not help.
Of course, when I run it the second time, the packages get installed (because all the packages to be installed using apt-get
are already there).
This was tested and is for Debian 6/Debian 7
How can I correct this error?
I have tried this workflow with my own script and everything works fine for me, although decoding/decompressing the string you provide fails with binascii.Error: Incorrect padding
.
The only suggestions I can make are:
r
before the string to make it raw format - the base64 format encoding could create escape sequences (although this is not a problem for the string you provided). str
decoded string variable - does this match the original script? If there was an &
at the end of the apt-get line, this would produce the behaviour you describe. Find what happens when you try the call with the original script ie
subprocess.call(['/bin/bash', '/usr/local/src/sample.sh'])
If this produces the same behaviour, check if it still happens running the script directly from bash. It could be some oddity that python introduces or some issue with the bash/apt configuration that causes things to go into the background (although I have no idea of any option that could do that and I know both fairly well).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.