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.

68 CHAPTER 4 Filling the toolbox<br />

download link (not a script) for my script to work properly. I have yet to find a case<br />

where such a link wasn’t available.<br />

The second new item in this script is the use of a regular expression in the sed substitution<br />

on the line fname¼$(echo $fname j sed "s/\.${extension}$/\.deb/").<br />

Recall that the most common use of sed is to replace one string <strong>with</strong> another.<br />

So far, we search for an exact phrase. Sed expects a regular expression to be used<br />

in searches. The period is a special character that matches any character in regular<br />

expressions. We must escape it <strong>with</strong> a backslash (\) to tell sed that we really mean a<br />

period. Our search string “\.${extension}$” matches a period, followed by the contents<br />

of the extension variable if <strong>and</strong> only if it occurs at the end of the line thanks to<br />

the trailing dollar sign. This period plus extension is replaced <strong>with</strong> “.deb”:<br />

#!/bin/bash<br />

# This script will iterate over the packages-to-download.txt<br />

# file <strong>and</strong> ask the user to verify the correct comm<strong>and</strong> to download<br />

# <strong>and</strong> then install the package.<br />

#<br />

# Originally created by Dr. Phil Polstra<br />

# for the book<br />

# <strong>Hacking</strong> <strong>and</strong> <strong>Penetration</strong> <strong>Testing</strong> With <strong>Low</strong> <strong>Power</strong> <strong>Devices</strong><br />

# we need wget<br />

WGET¼'which wget'<br />

#check for our file<br />

[ -e packages-to-download.txt ] jj ( echo "packages-to-download.txt\<br />

not found" && exit -1 )<br />

# This function will verify URL <strong>and</strong> then download a pkg<br />

dl_pkg () {<br />

echo -n\<br />

"Enter the correct URL to download $pkg or press Enter for $url> "<br />

read resp<br />

# if they didn't just press update the $url<br />

if [ "$resp" !¼ "" ] ; then<br />

$url ¼ $resp<br />

fi<br />

fname¼$(echo $url j tr "/" "\n" j tail -1)<br />

# download using wget<br />

if [ ! -e "/tmp/${fname}" ] ; then<br />

‘$WGET ${url} -O /tmp/${fname}‘<br />

fi<br />

}<br />

#install the package<br />

install_pkg () {

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

Saved successfully!

Ooh no, something went wrong!