Sep 272017
 

I wrote a little snippet to update my PXE server’s Debian netboot image so I can schedule this to run every month or so to ensure that my Netboot image is up to date.

The variables are to set the Debian mirror, architecture, and paths and filenames for the downloaded image file, and where the image file should be un-tarred to.
I have architecture in there because I had some old machines which require i386 kernels. If you don’t need i386, then you can replace all the arch variables and hardcode amd64 instead.

#!/bin/bash
debmirror=http://mirror.internode.on.net/pub/debian
arch=amd64
tmpfile=/tmp/$arch-netboot.tar.gz
tftppath=/srv/tftp/deb-stable/$arch
wget $debmirror/dists/stable/main/installer-$arch/current/images/netboot/netboot.tar.gz -O $tmppath
tar -C $tftppath -xzf $tmpfile ./debian-installer/amd64/initrd.gz ./debian-installer/amd64/linux --strip-components=3

Share
Jun 182015
 

I recently had to verify a whole bunch of servers were responding to SNMP and had the correct DNS reverse look ups in Active Directory.
I used this little script so that it would ask me for an IP address and then do a ping, and then if a server was reachable over ICMP, a nslookup if a server responded to the SNMP Query for the system name.
Continue reading »

Share
Nov 082013
 

I was adding some new features on PiParted recently and needed a simple file browser for my PiParted script. I couldn’t find anything online for this sort of thing, so I built my own file browser of sorts.

Using whiptail and the output of ls (which isn’t recommended but I’ll get around to posting an updated version of this script sometime), I managed to get a simple file browser to allow a user to pick a file.
I’ll paste bits and pieces of the code to explain it, and then paste the whole function at the end of this post.

This starts off with an if statement to check to see whether a path has been passed to the script. If it hasn’t it will start the listing off at /, Otherwise it will start at the path it had been passed.

function Filebrowser() {
if [ -z $1 ]; then
imgpath=$(ls -lhp / | awk -F ' ' ' { print $9 " " $5 } ')
else
imgpath=$(ls -lhp "/$1" | awk -F ' ' ' { print $9 " " $5 } ')
fi

Next, still checking to see whether it was passed a path, it will show a back button if it was passed a path.

if [ -z $1 ]; then
pathselect=$(whiptail --menu "Select Image File" 40 50 30 --cancel-button Cancel --ok-button Select $imgpath 3>&1 1>&2 2>&3)
else
pathselect=$(whiptail --menu "Select Image File" 40 50 30 --cancel-button Cancel --ok-button Select ../ BACK $imgpath 3>&1 1>&2 2>&3)
fi

Next, it checks the return value to make sure it was 0 to signify the user didn’t press cancel
After that, it checks the path the user selected to see if it was a directory, if it is then it re-runs the function and passes a path
Otherwise it will determine whether the file selected is the file type that is required, and if it is show that it is about to perform an action. If it’s not the type required, present an error to the user.

RET=$?
if [ $RET -eq 0 ]; then
if [[ -d "/$1$pathselect" ]]; then
Filebrowser "/$1$pathselect"
elif [[ -f "/$1$pathselect" ]]; then
fileout=`file "$1$pathselect"
filename=`readlink -m $1$pathselect`
if [[ $fileout =~ x86\ boot\ sector$ ]]; then
whiptail --yesno --title "! WARNING !" "About to try and automatically resize $filename. Are you sure ?" 10 50
### This bit of the code would be what you want to do after the user has selected the right type of file.
if [ $? -ne 0 ]; then
Filebrowser
fi
else
whiptail --msgbox --title "! ERROR ! ERROR ! ERROR ! " "Selected file is not an image file." 8 44
Filebrowser
fi

And lastly, check to make sure the path that was passed to the script is valid. At the moment, I don’t imagine this script would handle spaces well so you may see this error message when processing directories with spaces in their names.

else
echo pathselect $1$pathselect
whiptail --title "! ERROR !" --msgbox "Error setting path to image file." 8 44
unset base
unset imgpath
Filebrowser
fi
exit 0

And, as promised, the whole code in one big block

Filebrowser() {
if [ -z $1 ]; then
imgpath=$(ls -lhp / | awk -F ' ' ' { print $9 " " $5 } ')
else
imgpath=$(ls -lhp "/$1" | awk -F ' ' ' { print $9 " " $5 } ')
fi
if [ -z $1 ]; then
pathselect=$(whiptail --menu "Select Image File" 40 50 30 --cancel-button Cancel --ok-button Select $imgpath 3>&1 1>&2 2>&3)
else
pathselect=$(whiptail --menu "Select Image File" 40 50 30 --cancel-button Cancel --ok-button Select ../ BACK $imgpath 3>&1 1>&2 2>&3)
fi
RET=$?
if [ $RET -eq 1 ]; then
## This is the section where you control what happens when the user hits Cancel
AdvancedMenu
exit 0
elif [ $RET -eq 0 ]; then
if [[ -d "/$1$pathselect" ]]; then
Filebrowser "/$1$pathselect"
elif [[ -f "/$1$pathselect" ]]; then
## Do your thing here, this is just a stub of the code I had to do what I wanted the script to do.
fileout=`file "$1$pathselect"`
filename=`readlink -m $1$pathselect`
if [[ $fileout =~ x86\ boot\ sector$ ]]; then
whiptail --yesno --title "! WARNING !" "About to try and automatically resize $filename. Are you sure ?" 10 50
if [ $? -ne 0 ]; then
Filebrowser
fi
else
whiptail --msgbox --title "! ERROR ! ERROR ! ERROR ! " "Selected file is not an image file." 8 44
Filebrowser
fi
else
echo pathselect $1$pathselect
whiptail --title "! ERROR !" --msgbox "Error setting path to image file." 8 44
unset base
unset imgpath
Filebrowser
fi
exit 0
fi
}

Share
Aug 182013
 

I recently had to create a whole bunch of directories for my XBMC HTPC so that it could parse the directory contents correctly, so I made a little script to help me do it

This script takes a file extension as an argument, and then creates directories for all the files that have that extension. The directory name is the same as the original file name, minus the extension.

Example Usage: ./convert.sh avi
The example will get all avi files in the current directory and create directories for the files, and then move the files into those directories.

#/bin/bash
OldIFS=$IFS
IFS=$'\n'
shopt -s nullglob
for x in *.$1; do
mkdir ${x:0:(-4)}
mv $x ${x:0:(-4)}
done
IFS=$OldIFS

Share
Mar 102013
 

There’s been a few threads recently on the Raspberry Pi forums regarding SD Card images too big to fit on other SD cards.
So I’ve come up with a script to automatically resize SD Cards to the smallest size possible. At the moment it is Linux only unfortunately, but I may release a windows version if there’s demand.

The script can be downloaded from here

or copied and pasted from here

#!/bin/bash
# Automatic Image file resizer
# Written by SirLagz
strImgFile=$1


if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "**********************************"
echo "*** This should be run as root ***"
echo "**********************************"
echo ""
exit
fi

if [[ -z $1 ]]; then
echo "Usage: ./autosizer.sh "
exit
fi

if [[ ! -e $1 || ! $(file $1) =~ "x86" ]]; then
echo "Error : Not an image file, or file doesn't exist"
exit
fi

partinfo=`parted -m $1 unit B print`
partnumber=`echo "$partinfo" | grep ext4 | awk -F: ' { print $1 } '`
partstart=`echo "$partinfo" | grep ext4 | awk -F: ' { print substr($2,0,length($2)-1) } '`
loopback=`losetup -f --show -o $partstart $1`
e2fsck -f $loopback
minsize=`resize2fs -P $loopback | awk -F': ' ' { print $2 } '`
minsize=`echo $minsize+1000 | bc`
resize2fs -p $loopback $minsize
sleep 1
losetup -d $loopback
partnewsize=`echo "$minsize * 4096" | bc`
newpartend=`echo "$partstart + $partnewsize" | bc`
part1=`parted $1 rm 2`
part2=`parted $1 unit B mkpart primary $partstart $newpartend`
endresult=`parted -m $1 unit B print free | tail -1 | awk -F: ' { print substr($2,0,length($2)-1) } '`
truncate -s $endresult $1


Please support the continued development of any useful scripts and tutorials that you have found useful !




Share