DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 15th January 2010
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 194
Thanked 47 Times in 22 Posts
Default Script to check md5 hash on FreeBSD

Recently I needed to check some md5 hashes of some files on NetBSD. I started toying with awk, and just glanced on a cksum(1) man page. There was the "-c" option staring at me, which is being used to check md5 hashes from a file. The option was not actually available until NetBSD 4.0. Reading further on the net, I discovered that FreeBSD does not have that option (OpenBSD cksum has the "-c" option). One could get the option by installing md5sum (from sysutils/coreutils), but it is not in base system. To cut the long story short, here is the code that someone might find useful:
Code:
#!/bin/sh
# awk code for: cksum -c MD5
        md5 "$1" | \
        awk '
         NR==FNR{
                a[$2]=$4
                next
        }
        ($2 in a){
                if(a[$2] == $4)
                        print "OK: " $2
                else
                        print "ERROR: " $2
        }' - "$2"
Example:
Code:
$ md5 file.iso >CKSUM.MD5          
$ cat CKSUM.MD5                       
MD5 (file.iso) = b76561047748615d9a6a40da795fac3d
$ ./md5_check.sh file.iso
OK: (file.iso)
$
Script could be useful for those that don't have gmd5sum (i.e. GNU md5) installed, and they want to check the md5 hash of a FreeBSD iso file they just downloaded.
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD

Last edited by s0xxx; 17th January 2010 at 12:10 AM.
Reply With Quote
  #2   (View Single Post)  
Old 17th January 2010
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 194
Thanked 47 Times in 22 Posts
Default

I somehow got into a hurry when I was writing this so I guess I didn't pick a good example. So hopefully here is a better one; say you want to donwload a FreeBSD iso file. There is a file in the same directory called CHECKSUM.MD5 that holds md5 hashes of files so you could check the integrity. Off course, as a smart guy you always check the file:
Code:
$ ftp ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/8.0/8.0-RELEASE-i386-disc1.iso
$ ftp ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/8.0/CHECKSUM.MD5
$ ls
8.0-RELEASE-i386-disc1.iso       CHECKSUM.MD5       md5_check.sh
$ ./md5_check.sh 8.0-RELEASE-i386-disc1.iso CHECKSUM.MD5
OK: (8.0-RELEASE-i386-disc1.iso)
$
Script is much useful if you got more files, say a FreeBSD source:
Code:
$ ftp -V ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/8.0-RELEASE/src/
Trying 87.51.34.132...
ftp> get CHECKSUM.MD5
ftp> mget *.a*
mget sbase.aa [anpqy?]? a
Prompting off for duration of mget.
^C
ftp> 
ftp> quit
$ ll
total 16930
drwxrwxrwt   2 soxxx   soxxx  -     360 Jan 16 23:52 ./
drwxr-xr-x  21 soxxx   soxxx  -     512 Jan 15 15:54 ../
-rw-r--r--   1 soxxx  soxxx  -    5758 Nov 21 17:07 CHECKSUM.MD5
-rwxrwxrwx   1 soxxx  soxxx  -     192 Jan 16 23:31 md5_hash.awk*
-rw-r--r--   1 soxxx  soxxx  -   75919 Nov 21 17:07 sbase.aa
-rw-r--r--   1 soxxx  soxxx  -  521808 Nov 21 17:07 sbin.aa
-rw-r--r--   1 soxxx  soxxx  -  976127 Nov 21 17:07 scddl.aa
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.aa
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ab
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ac
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ad
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ae
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.af
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ag
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ah
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ai
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.aj
-rw-r--r--   1 soxxx  soxxx  - 1425408 Nov 21 17:07 scontrib.ak
-rw-r--r--   1 soxxx  wheel  -       0 Jan 16 23:51 scontrib.al
-rw-r--r--   1 soxxx  wheel  -   55920 Jan 16 23:51 scontrib.am
$ ./md5_hash.awk "s*" CHECKSUM.MD5                                                                                                             
OK: (sbase.aa)
OK: (sbin.aa)
OK: (scddl.aa)
OK: (scontrib.aa)
OK: (scontrib.ab)
OK: (scontrib.ac)
OK: (scontrib.ad)
OK: (scontrib.ae)
OK: (scontrib.af)
OK: (scontrib.ag)
OK: (scontrib.ah)
OK: (scontrib.ai)
OK: (scontrib.aj)
OK: (scontrib.ak)
ERROR: (scontrib.al)
ERROR: (scontrib.am)
$
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
please check my pf.conf gosha OpenBSD Security 10 30th January 2009 12:32 AM
"free" command/perl script for freebsd unixdude FreeBSD General 0 17th November 2008 09:23 PM
Best way to check if freebsd server is running a nameserver service/daemon Yuka FreeBSD General 7 6th November 2008 01:26 AM
check for badblocks ccc FreeBSD General 5 30th October 2008 07:00 PM
relayd (and hoststated) give syntax error for 'check script' gwl OpenBSD Security 2 2nd May 2008 04:53 PM


All times are GMT. The time now is 11:55 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick