Solved

AIX 5.3 Shell Script

Posted on 2013-06-05
9
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

762 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