Solved

AIX 5.3 Shell Script

Posted on 2013-06-05
9
698 Views
Last Modified: 2013-06-05
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"
0
Comment
Question by:Bryant
  • 5
  • 4
9 Comments
 

Author Comment

by:Bryant
ID: 39223425
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

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39223516
Hmm... I wonder how the servername is getting dropped.  Let me do a quick test.
0
 

Author Comment

by:Bryant
ID: 39223527
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.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39223541
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

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Bryant
ID: 39223644
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\"
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39223726
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)
0
 

Author Comment

by:Bryant
ID: 39223835
#!/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.
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 39223848
The quote in the xargs command looks funny to me, but if its working, it's working! ;-)

#!/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

0
 

Author Closing Comment

by:Bryant
ID: 39223897
Thanks nemws1, once again you were a big help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now