Sign in to follow this  
Followers 0
TheFunk

My Code...It's horrifying. Want to run it?

4 posts in this topic

I've been writing an OS detection script that finds hard drives, checks the partitions and then reports information about them to stdout. I plan to fix this code tomorrow so that it looks prettier, and reports much more than just OS info to a log file (eg specific partition size, file system type, partition label, etc). If anyone wants to run it as is though, please be my guest. Feedback would be nice as well. Right now it's ridiculously incomplete, but tomorrow there will be much more (hopefully). You must run it as root! B)

tldr: Here do this!

su

./osdetector

#!/bin/bash

# This script is TheFunk's kinda shitty disk/OS detector.
# In the years to come this is definitely going to be rewritten in
# something more convenient.
# Also TheFunk didn't write any pretty GUI that might be related to this. TheFunk is terrible with visuals.


# First we use parted/egrep to find available disks, their location in /dev, and their sizes.
# Then sed does a little corrective formatting and we save everything to a text file.

clear
echo "In a moment you'll see a list of available drives, and information about them."
sleep 3
parted -l | egrep '/dev/(s|h)d[a-z]' | sed 's/://' > thesemahdisks

# Next we pause before examining the disks. This can be removed
# and done automatically, but I figured it would be good for stopping
# the script. Also I'm lazy, what else is new?

clear
echo "It would appear as if you have `cat thesemahdisks | wc -l` disks installed in, or otherwise plugged into this system."
echo ""
echo "Now we'll examine those disks. Please press Enter."
read hold
clear
disknumber=1
totesdisks=`cat thesemahdisks | wc -l`
looper=true
# Here's where things get weird. Bash used to hate incrementing numbers.
# This was giving all sorts of weird output earlier. It appears to be
# working sorta correctly now though. After some hair pulling...
while [ $looper ]
do

if [ "$disknumber" -le "$totesdisks" ];
then
examinee=`sed -n "$disknumber p" thesemahdisks | cut -d' ' -f2`
# echo parted $examinee print *This line can be removed.*
echo ""
totesparts=`parted $examinee print | cut -c 2 | sed 's/[A-Za-z]*//g' | sed '/^$/d' | wc -l`
echo "Disk number " $disknumber " has " $totesparts " partitions."
echo ""
mkdir -p /mnt/temp
currentpart=1
newlooper=true
while [ $newlooper ]
do

if [ "$currentpart" -le "$totesparts" ];
then
mount $examinee$currentpart /mnt/temp
if [ -f /mnt/temp/bootmgr ];
then
echo "OS for partition " $examinee$currentpart " is Windows Vista or 7."
elif [ -f /mnt/temp/ntldr ];
then
echo "OS for partition " $examinee$currentpart " is Windows XP or older."
# Kinda borrowed this.
# Thanks Internet.
elif [ -f /mnt/temp/etc/debian_version ];
then
echo "OS for partition " $examinee$currentpart " is Debian Linux."
elif [ -f /mnt/temp/etc/redhat-release ];
then
echo "OS for partition " $examinee$currentpart "is RedHat Linux."
elif [ -f /mnt/temp/etc/SuSE-release ];
then
echo "OS for partition " $examinee$currentpart " is OpenSuSE Linux."
# /end partly borrowed code
elif [ -f /mnt/temp/etc/lsb-release ];
then
echo "Ignore the next line if it's a repeat."
echo "OS for partition " $examinee$currentpart "is " `cat lsb-release | cut -d"=" -f2 | sed -n "1 p"`
else
echo "Could not find OS information for partition " $examinee$currentpart
echo "Moving to next partition."
sleep 2
fi
umount /mnt/temp 2> errorlog
((currentpart++))
else
echo ""
echo "We're either all done, or there are no partitions here brah."
echo ""
sleep 2
break
fi
newlooper=false
done
((disknumber++))
else
echo "We're all done mayne."
sleep 2
break
fi
looper=false
done
echo ""
echo "We're done here."
echo "Press Enter to end the program"
read hold
clear
echo "Quitting in...3 seconds"
sleep 1
clear
echo "Quitting in...2 seconds"
sleep 1
clear
echo "Quitting in...1 second"
sleep 1
exit 0

0

Share this post


Link to post
Share on other sites

Runs, but does not properly detect Arch Linux as an OS...which isn't surprising, as it's not in your elif block!

0

Share this post


Link to post
Share on other sites

Runs, but does not properly detect Arch Linux as an OS...which isn't surprising, as it's not in your elif block!

I added Gentoo, Fedora, and Arch today. I'm going to include a few of the BSD's and Solaris as well. Also I corrected some of the phrasing/redundancies, so now it actually seems halfway decent. I've been letting mount report swap space to the screen, but I should probably find a way to detect swap space, that way I don't get mount errors all over the place. Also the script now returns partition size/OS to a text file.

edit: typos, typos everywhere.

Edited by TheFunk
0

Share this post


Link to post
Share on other sites

#!/bin/bash

# This script is TheFunk's kinda shitty disk/OS detector.
# In the years to come this is definitely going to be rewritten in
# something more convenient.
# Also TheFunk didn't write any pretty GUI that might be related to this. TheFunk is terrible with visuals.


# First we use parted/egrep to find available disks, their location in /dev, and their sizes.
# Then sed does a little corrective formatting and we save everything to a text file.
echo "Disk Information" > infofile
clear
echo "In a moment you'll see a list of available drives."
sleep 2
parted -l | egrep '/dev/(s|h)d[a-z]' | sed 's/://' > thesemahdisks

# Next we pause before examining the disks. This can be removed
# and done automatically, but I figured it would be good for stopping
# the script. Also I'm lazy, what else is new?

clear
echo "It would appear as if you have `cat thesemahdisks | wc -l` disks installed in, or otherwise plugged into this system."
echo ""
echo "Now we'll examine those disks. Please press Enter."
read hold
clear
disknumber=1
totesdisks=`cat thesemahdisks | wc -l`
looper=true
# Here's where things get weird. Bash used to hate incrementing numbers.
# This was giving all sorts of weird output earlier. It appears to be
# working sorta correctly now though. After some hair pulling...
while [ $looper ]
do

if [ "$disknumber" -le "$totesdisks" ];
then
examinee=`sed -n "$disknumber p" thesemahdisks | cut -d' ' -f2`
# echo parted $examinee print *This line can be removed.*
echo ""
totesparts=`parted $examinee print | cut -c 2 | sed 's/[A-Za-z]*//g' | sed '/^$/d' | wc -l`
echo "Disk number " $disknumber " has " $totesparts " partitions."
echo ""
mkdir -p /mnt/temp
currentpart=1
newlooper=true
while [ $newlooper ]
do

if [ "$currentpart" -le "$totesparts" ];
then
mount $examinee$currentpart /mnt/temp 2> errorlog
echo $examinee$currentpart >> infofile
blkid $examinee$currentpart | tr ' ' '\n' | grep TYPE >> infofile
parted $examinee$currentpart print | tail -2 | tr ' ' '\n' | sed '/^$/d' | sed -n '4p' >>infofile

if [ -f /mnt/temp/bootmgr ];
then
echo "OS: Windows Vista or 7." >> infofile
elif [ -f /mnt/temp/ntldr ];
then
echo "OS: Windows XP or older." >> infofile
# Kinda borrowed this.
# Thanks Internet.
elif [ -d /mnt/temp/pentest ];
then
echo "OS: Backtrack Linux" >> infofile

elif [ -f /mnt/temp/etc/debian_version ];
then
echo "OS: Debian Linux." >> infofile

elif [ -f /mnt/temp/etc/redhat-release ];
then
echo "OS: RedHat Linux." >> infofile

elif [ -f /mnt/temp/etc/SuSE-release ];
then
echo "OS: OpenSuSE Linux." >> infofile
# /end partly borrowed code
elif [ -f /mnt/temp/etc/pacman.conf ];
then
echo "OS: Arch Linux" >> infofile

elif [ -d /mnt/temp/etc/portage ];
then
echo "OS: Gentoo Linux." >> infofile

elif [ -f /mnt/temp/etc/lsb-release ];
then
echo "OS: "`cat lsb-release | cut -d"=" -f2 | sed -n "1 p"` >> infofile

else
echo "OS: Unknown" >> infofile
sleep 2
fi
umount /mnt/temp 2> errorlog
((currentpart++))
else
echo ""
echo "All done with "$examinee", moving to next disk."
echo ""
sleep 1
break
fi
newlooper=false
done
((disknumber++))
else
echo "All disks successfully checked. Information logged to infofile."
sleep 3
break
fi
looper=false
done
echo ""
echo "Press Enter to end the program"
read hold
rm errorlog
rm thesemahdisks
clear
echo "Quitting in...3 seconds"
sleep 1
clear
echo "Quitting in...2 seconds"
sleep 1
clear
echo "Quitting in...1 second"
sleep 1
exit 0

The code now logs everything to the file infofile. Before running it make sure you have blkid and parted installed. I removed Fedora (for now) until I can figure out a better way to detect it (RedHat was showing up as Fedora, and vice versa). The code should now also detect filesystem type, and partition size, if anyone notices any issues let me know. I'm aware that the code hates extended partitions.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0