Solved

AIX 5.3 Shell Script

Posted on 2013-06-05
9
700 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

932 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

7 Experts available now in Live!

Get 1:1 Help Now