Link to home
Start Free TrialLog in
Avatar of Bryant
BryantFlag for United States of America

asked on

AIX 5.3 Shell Script

Hi, nemws1 has been helping with this so far, please allow this expert to continue this support.

I have a script below that gives me the results of the last command in a file that is emailed to me. Right now I am using sendmail to exectute the mail and using uuencode to help attach the file. When the script ssh to different servers I also need to be able to get the results of this command 'tail -1 /directory/filename' to show up on the report. I need the results to show up on the same column as the results I am getting when I run teh script now.

servername  user   date   time   duration   results of the file.

Can this script be altered to fit what I am trying to accomplish?

#!/bin/bash
time_start=`date +%s`
for i in $(cat master/store_numbers)
do
    ssh -o BatchMode=yes XXXX.$i 'last' |
    egrep -v "^(reboot|shutdown|root|nopriv|co1)" | \
    awk '{print $1","$4" "$5","$6","$9}' | \
    head -1 | \
    xargs -I% echo "$i,%"
done
time_end=`date +%s`
time_exec=`expr $(( $time_end - $time_start ))`
echo "Execution time is $time_exec seconds"
Avatar of Bryant
Bryant
Flag of United States of America image

ASKER

This is the entire script and it works fine thanks to nemws1 but the results is not showing me all the information I need. My report is displaying line 1 but I need for it to display like line 2.

1.        user      date      time      duration      results from tail -1  /directory/filename      
2. server#       user      date      time      duration      results from tail -1  /directory/filename      

#!/bin/bash
# Delete old report and prepare new one
rm -f /directory/filename.*
OUTPUT=/directory/filename.csv
DATE=`date`
time_start=`date +%s`
for i in $(cat directory/filename)
do
    ssh -o BatchMode=yes XXXX.$i "
    REL=\`tail -1 /directory/filename\`;
    last |
    egrep -v \"^(reboot|shutdown|root|nopriv|co1)\" |
    awk '{print \$1\",\"\$4\" \"\$5\",\"\$6\",\"\$9}' |
    head -1 |
    xargs -I% echo \"\$i,%,\$REL\"
    "
done > $OUTPUT
time_end=`date +%s`
time_exec=`expr $(( $time_end - $time_start ))`
echo "Execution time is $time_exec seconds"
# Send the mail
uuencode filename.csv filename.csv | mail -s "Subject" newbie@scripting.com

Open in new window

Avatar of Nem Schlecht
Hmm... I wonder how the servername is getting dropped.  Let me do a quick test.
Avatar of Bryant

ASKER

I changed this

xargs -I% echo \"\$i,%,\$REL\"
 
to  

xargs -I% echo $i,%,\$REL\"
 
and it showed back up. If you post the entire code with what I changed at the end I will accept your solution. Thanks so much for your help with this.
So this worked?

#!/bin/bash
# Delete old report and prepare new one
rm -f /directory/filename.*
OUTPUT=/directory/filename.csv
DATE=`date`
time_start=`date +%s`
for i in $(cat directory/filename)
do
    ssh -o BatchMode=yes XXXX.$i "
    REL=\`tail -1 /directory/filename\`;
    last |
    egrep -v \"^(reboot|shutdown|root|nopriv|co1)\" |
    awk '{print \$1\",\"\$4\" \"\$5\",\"\$6\",\"\$9}' |
    head -1 |
    xargs -I% echo \"$i,%,\$REL\"
    "
done > $OUTPUT
time_end=`date +%s`
time_exec=`expr $(( $time_end - $time_start ))`
echo "Execution time is $time_exec seconds"
# Send the mail
uuencode filename.csv filename.csv | mail -s "Subject" newbie@scripting.com

Open in new window

Avatar of Bryant

ASKER

You have to remove the first '\' in line 15 and it works perfectly. Can you post again with the correction and i will accept the solution.


xargs -I% echo "$i,%,\$REL\"
Weird!  Can you double-check.  If I do that, the command will end early.  Can you cut-paste your code again (like you, I want the correct solution posted)
Avatar of Bryant

ASKER

#!/bin/bash

# clean any old output files and initialize a new one
rm -f /directory/folder/filename.*
OUTPUT=/directory/folder/filename.csv
DATE=`date`
time_start=`date +%s`
for i in $(cat folder/filename)
do
    ssh -o BatchMode=yes xxxx.$i "
    REL=\`tail -1 /directory/folder/folder/filename\`;
    last |
    egrep -v \"^(reboot|shutdown|root|nopriv|co1)\" |
    awk '{print \$1\",\"\$4\" \"\$5\",\"\$6\",\"\$9}' |
    head -1 |
    xargs -I% echo $i,%,\$REL\"
    "
done > $OUTPUT
time_end=`date +%s`
time_exec=`expr $(( $time_end - $time_start ))`
echo "Execution time is $time_exec seconds"

# mail it out to team

uuencode filename.csv filename.csv | mail -s "subject" newbie@scripting.com

Open in new window





I copied it directly off the server im using it on. i only changed where you see 'directory, folder, filename, xxxx, co1, subject and email address. It just finished running on all of my servers as I typed this. It gave me the exact output that I needed. Completed on over 200 servers.
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bryant

ASKER

Thanks nemws1, once again you were a big help.