[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

AIX 5.3 Shell Script

Posted on 2013-06-05
9
Medium Priority
?
738 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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…
Suggested Courses

656 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