Need to create a simple script using MD5, SSH...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to create a simple script using MD5, SSH...
# 43  
Old 03-09-2011
That can't possibly have been the code you ran, a third of it's missing. I'm assuming you mean my second posted code?

Quote:
The only thing I added was #!/usr/bin/sh, cause my boss told me I would need it!
Not the way you're using it, you don't. The way you're using it you aren't even guaranteed /usr/bin/sh since you just type 'sh' and let your login shell run whatever sh comes first in your PATH. The #! line only matters when you run it like ./scriptname

The md5sum utility is part of openssh and found in many places. This md5 utility you have, I have no idea what it is; but with it's weird location( /usr/local/bin/ is where custom-built things usually get installed), its poor error behavior(dumping errors to stdout), and the vexing way it reads from stdin even when given a filename, it looks like a one-off to me -- not even a standard HP-UX utility. So how it actually behaves is something I've had to deduce like the blind man and the elephant.

I think that last code may actually have been working. Smilie It just never printed anything unless a host found an error. Quick, try this.

Code:
#!/usr/bin/sh
# If you don't have mktemp, TMP=/tmp/$$ will do.  mktemp is more secure.
TMP=`mktemp`
while read fname; do 
    #bname=$(basename $fname) 
    echo "trying ${fname}" >&2

    if ! /usr/local/bin/md5 "$fname" > "$TMP" </dev/null
    then
        echo "md5 on ${fname} failed: "
        # print the error
        cat "$TMP"
        continue
    fi

    hash=$(awk '{print $4}' "$TMP")

    echo "local hash is ${hash}" >&2

    # should just print the md5's
    /usr/local/bin/hosts | while read box
    do
        # redirect its stdin to prevent it reading any filenames
        ssh "${box}" "/usr/local/bin/md5" "${fname}" > "$TMP" </dev/null
        # Skip errors
        [ "$?" -eq 0 ] || continue

        remotehash=$(awk '{print $4;}' < "$TMP")
        if [ "$remotehash" = "$hash" ]
        then
          echo "${box} $fname is okay!"
        else
          echo "${box} $fname differs!"
        fi
    done

done < /home/izivanov/iz3

# clean up temp file
rm -f "$TMP"


Last edited by Corona688; 03-09-2011 at 04:19 PM..
# 44  
Old 03-09-2011
Sorry you're correct I posted before I checked what I copied to the reply box.

The code I ran right now:

Code:
#!/usr/bin/sh
# If you don't have mktemp, TMP=/tmp/$$ will do.  mktemp is more secure.
TMP=`mktemp`
while read fname; do 
    #bname=$(basename $fname) 
    echo "trying ${fname}" >&2

    if ! /usr/local/bin/md5 "$fname" > "$TMP" </dev/null
    then
        echo "md5 on ${bname} failed: "
        # print the error
        cat "$TMP"
        continue
    fi

    hash=$(awk '{print $4}' "$TMP")

    echo "local hash is ${hash}" >&2

    # should just print the md5's
    /usr/local/bin/hosts | while read box
    do
        # redirect its stdin to prevent it reading any filenames
        ssh "${box}" "/usr/local/bin/md5" "${fname}" > "$TMP" </dev/null
        # Skip errors
        [ "$?" -eq 0 ] || continue

        remotehash=$(awk '{print $4;}' < "$TMP")
        if [ "$remotehash" = "$hash" ]
        then
          echo "${box} $fname is okay!"
        else
          echo "${box} $fname differs!"
        fi
    done

done < /home/izivanov/iz3

# clean up temp file
rm -f "$TMP"

and the result looked almost like th eone I got with his script! YAY!

But there is a difference. THey can't all be okay because my sync report told me just this morning that they weren't!

Here is the result!

Quote:
xxxx(root):/home/izivanov# sh dirsync6.sh
trying /opt/dba/scp/xxxxxxxxxxxxxx
local hash is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx /opt/dba/scp/xxxxxxxxxxx is okay!
xxxx/opt/dba/scp/xxxxxxxxxxx is okay!

~
~
~
It looks like xxxxx never got matched!

Let me check a coupe of files and servers for MD5 just to be safe maybe they got updated!

Last edited by zixzix01; 03-09-2011 at 06:07 PM..
# 45  
Old 03-09-2011
Well, check them. The script says /opt/dba/bin/bkginit.relaybkg on deva2 differs -- does it? And look at a file your report says differs but this script doesn't. Your report may be checking things this isn't. This checks their contents, not their owners, not their timestamps, not their permissions.

---------- Post updated at 02:36 PM ---------- Previous update was at 02:36 PM ----------

Quote:
It looks like dsva never got matched!
Isn't that the system you're running it from? Comparing itself to itself would be pretty futile.
# 46  
Old 03-09-2011
Yup they do differ on dsva:
Quote:
dsva(root):/home/izivanov# | diff - "/opt/dba/bin/bkginit.relaybkg" <
75c75
< *imgb|*devb|*deva2) KERNEL="RELAYHEALTH";;
---
> *imgb|*devb) KERNEL="RELAYHEALTH";;
dsva(root):/home/izivanov#
Okay we got somewhere! Thank you so much!

---------- Post updated at 03:45 PM ---------- Previous update was at 03:43 PM ----------

So what changed I don't understand at all what you did!
# 47  
Old 03-09-2011
Think they all match because $TMP still has result from local hash or previous host - probably safest to clear out $TMP on each loop
Also fname should be passed as a quoted string on the md5 command line not a seperate parameter on the ssh command line.

Change this line:
Code:
 ssh "${box}" "/usr/local/bin/md5" "${fname}" > "$TMP" </dev/null

to:
Code:
> $TMP
ssh "${box}" "/usr/local/bin/md5 \"${fname}\" " > "$TMP" </dev/null


Last edited by Chubler_XL; 03-09-2011 at 05:08 PM..
# 48  
Old 03-09-2011
Okay, so we're feeding the output of /usr/local/bin/hosts into the loop like this:

Code:
/usr/local/bin/hosts | while read LINE
do
        echo "Got $LINE"
done

...but anything in the loop can read read from that, not just the 'while' bit. If you do this:
Code:
/usr/local/bin/hosts | while read LINE
do
        cat > /dev/null # Read and throw away the rest of the lines
        echo "Got $LINE"
done

...you get nothing but the first line.

I didn't expect the md5 command to read anything except the file it was given; that's not just the way md5sum works but the traditional way for UNIX commands to operate. Some read standard input when given no files, and some read standard input when told to read from -. I gave it a filename, and it read from the file, and when it finished that it read from the list, too! Throwing it all away and putting it into the checksum. I had to force it to read from /dev/null instead, which is a "special" file that always does nothing.

---------- Post updated at 03:11 PM ---------- Previous update was at 03:02 PM ----------

Quote:
Originally Posted by Chubler_XL
Think they all match because $TMP still has result from local hash or previous host
Sorry, but that's completely wrong. > always truncates the file, so the temp file was always completely replaced each time ssh was called. You could hardly expect your ">$TMP" to work if > didn't work that way! You have to use >> to append.

The defensive quoting is a good idea though.
# 49  
Old 03-09-2011
Thanks for your input Chubler, I did put in those corrections and still the same result although, although I don't really understand how to clear the $TMP on every loop!


Sorry Corona, defensive quoting?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

2. Shell Programming and Scripting

Create simple script

Dear all, I have a directory named A and some subdirectories named B, C, D with .xml files. I want to use the following command to strip the file. sed -re ':start s/<*>//g; /</ {N; b start}' file.xml > file.xml At the same time, I want to remove the blank lines using sed '/^$/d' How can... (6 Replies)
Discussion started by: corfuitl
6 Replies

3. Shell Programming and Scripting

How to create a simple copy script?

Guys I want to do this: copy: /var/router/system1/config/backup/install.put /var/router/system2/config/backup/install.put /var/router/system3/config/backup/install.put /var/router/system4/config/backup/install.put into: /var/router/system1/config/install.dat... (22 Replies)
Discussion started by: DallasT
22 Replies

4. Shell Programming and Scripting

Create md5 sums and archive the resulting md5 files

Hello everyone, I am looking to basically creating md5sum files for all iso files in a directory and archive the resulting md5 files into a single archive in that very same directory. I worked out a clumsy solution such as: #find files for which md5sum are to be created and store the... (1 Reply)
Discussion started by: SurfTranquille
1 Replies

5. Shell Programming and Scripting

How to create a simple shell script to backup

Hello - I am in process of deleting many files which are older than 4 weeks. For example I am inside: /subsystem/prod/ Files are with various extentions, but anything older than 4 weeks should be deleted. What would be the most simplest script to acheive this? (4 Replies)
Discussion started by: DallasT
4 Replies

6. Solaris

How to create a simple background script on Solaris

I have a local account for a unix server. The idle timeout for the account is around 10 mins. I have to login to the server multiple times during the day. Is there a way to increase the idle timeout or may be a script that I can run on background so it is not idle. Something like echo date every 9... (3 Replies)
Discussion started by: vinaysa
3 Replies

7. Shell Programming and Scripting

Simple SSH script

I have several unix servers, I need to logon to each server and find out if an id exists on that server. I need a simple script for this, i have come up with the following script, but I cannot bring the output of a child process on the remote server. for i in `cat SERVER_LIST` do ssh $i... (5 Replies)
Discussion started by: ramky79
5 Replies

8. Shell Programming and Scripting

Simple Script to create folders

Hi I want to write a small script that will create folders named from `AAAA' all the way to `ZZZZ'. That is: `AAAA' `AAAB' `AAAC' ... `AABA' `AABB' `AABC' ... `ABAA' `ABAB' `ABAC' ... `ABBA' ... `ZZZZ' (4 Replies)
Discussion started by: ksk
4 Replies

9. Shell Programming and Scripting

Modifying simple commands to create a script

Can anyone direct me to a resource that explains scripting in simple terms? I have visited many sites and browsed this forum and have yet to find simple explanations. (8 Replies)
Discussion started by: rocinante
8 Replies

10. Shell Programming and Scripting

How to create md5 Hash variable?

I have a script that runs the grub-md5-crypt command based on whether the pass_value variable is a non-zero string. The md5 hash is being created in the /opt/hostconfigs/$HOST file, but I can't echo $md5_value. It is blank. Is there a way to create and echo a md5 hash variable? if then... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question