10.09.2016 Views

Hacking_and_Penetration_Testing_with_Low_Power_Devices

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Adding tools the easy way<br />

67<br />

echo -n "Is the a Debian package or Source archive? [D]/S> "<br />

read resp<br />

if [ $resp ¼¼ 's' ] jj [ $resp ¼¼ 'S' ] ; then<br />

echo "${pkg} ${url}" >> source-to-download.txt<br />

else<br />

echo "${pkg} ${url}" >> packages-to-download.txt<br />

fi<br />

fi<br />

fi<br />

}<br />

# interate over the todo list<br />

# read whole file into an array<br />

declare -i count¼0<br />

while read pkg<br />

do<br />

pkg_list[${count}]¼${pkg}<br />

count+¼1<br />

done < "todo-packages.txt"<br />

# now call find_dpkg for each package<br />

# note that we have to do this instead of the more straightforward<br />

# method of reading in the values <strong>and</strong> calling find_dpkg directly<br />

# because that would conflict <strong>with</strong> read statements in find_dpkg<br />

for pkg in ${pkg_list[@]}<br />

do<br />

find_dpkg<br />

done<br />

The preceding script will create a file called packages-to-download.txt. Each line<br />

in this file contains a package name, followed by a URL to download the required<br />

Debian package file. The following script will iterate over this file <strong>and</strong> ask the user to<br />

verify the URL before downloading. Once a file has been successfully downloaded,<br />

it is sanity-checked to see if it is a .deb file. If it is not, the user is warned <strong>and</strong> given an<br />

opportunity to enter an uncompress or rename comm<strong>and</strong>. Finally, the package is<br />

installed using the comm<strong>and</strong> sudo dpkg -i /tmp/.<br />

If you have been following along, there are only two new techniques used in this<br />

script. The first is the use of the tr (translate) comm<strong>and</strong>. This is used twice in the<br />

script: first to strip the filename from the end of the URL <strong>and</strong> then to strip the file<br />

extension from the end of the filename.<br />

Let us break down the first line where tr is used: fname¼$(echo $url j tr "/" "\n"<br />

jtail-1). Recall that enclosing a comm<strong>and</strong> in $() causes the results of the comm<strong>and</strong> to<br />

be assigned to a variable, fname in this case. The first comm<strong>and</strong> echo $url prints out<br />

the URL <strong>and</strong> pipes it to the tr comm<strong>and</strong>. The comm<strong>and</strong> tr “/” “\n” causes every slash<br />

to be replaced <strong>with</strong> a newline character. This effectively breaks each part of the URL<br />

out onto a separate line. These lines are then piped to the tail-1 comm<strong>and</strong> that returns<br />

only the last line, which is everything after the last /. Note that you need a direct

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!