Jump to content


Photo
- - - - -

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


  • Please log in to reply
3 replies to this topic

#1 TheFunk

TheFunk

    SUP3R 31337

  • Binrev Financier
  • 187 posts
  • Country:
  • Gender:Male

Posted 18 July 2012 - 09:56 PM

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. <img src='http://www.binrev.com/forums/public/style_emoticons/<#EMO_DIR#>/;).gif' class='bbc_emoticon' alt=';)' />


# 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


#2 systems_glitch

systems_glitch

    Dangerous free thinker

  • Moderating Team
  • 1,668 posts
  • Gender:Male

Posted 19 July 2012 - 07:27 AM

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

#3 TheFunk

TheFunk

    SUP3R 31337

  • Binrev Financier
  • 187 posts
  • Country:
  • Gender:Male

Posted 19 July 2012 - 01:18 PM

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, 19 July 2012 - 01:21 PM.


#4 TheFunk

TheFunk

    SUP3R 31337

  • Binrev Financier
  • 187 posts
  • Country:
  • Gender:Male

Posted 05 August 2012 - 04:43 PM

#!/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. <img src='http://www.binrev.com/forums/public/style_emoticons/<#EMO_DIR#>/;).gif' class='bbc_emoticon' alt=';)' />


# 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.




BinRev is hosted by the great people at Lunarpages!