Solved

read file script not working

Posted on 2009-07-13
7
240 Views
Last Modified: 2013-12-06
I have created a small script on a RHE5 server that is supposed to read a list of servers from a file 'one at a time' and run some functions then return to read the next server and continue. It is running the first server then stopping.

Also I am trying to create the report file as $hostname.before but not working also.

/mnt;/mnt/sysconf.sh > /mnt/$HOST.before    <---------------

Any help would be appreciated...
get_before-cfgs.sh
 

======================START====================

#!/bin/sh

#get_before-cfgs.sh
 

set -x

$HOST='hostname -s'

N=0

while read server

do

N=$((N+1))

echo $server

ssh root@"$server" 'mount lucy:/usr/local/bin/UX_get_cfgs /mnt;/mnt/sysconf.sh > /mnt/$HOST.before;umount /mnt'

done < servers.txt
 

=============================STOP================================
 
 
 

Servers.txt
 

==============================START=============================
 

CYPRESS

DOGWOOD

FILBERT

HACKBERRY

WILLOW

CASCARA

CHERRY

CHESTNUT

CATALPA

CHINKAPIN

COTTONWOOD

BALSAM

BASSWOOD

ASPEN

BUCKEYE

BUCKTHORN

SEQUOIA

SPRUCE

SWEETGUM

SYCAMORE

TANOAK

LOCUST

MADRONE

POPLAR

REDCEDAR

REDWOOD

WALNUT

HAWTHORN

HAZEL

JUNIPER

HOLLY

LARCH
 

==========================STOP==========================
 

sysconfig.sh
 

=====================START=============================
 

#!/bin/sh

# Simple sysconfig script
 

set -x
 

rm -rf /tmp/config.servername
 

echo  "******************************************"

echo  "         System Configuration    "

echo  "******************************************"
 

uname -a
 

echo  "******************************************"

echo  "             CPU & Memory        "

echo  "******************************************"
 
 

echo  "*************/proc/version****************"

cat /proc/version

echo  "*************/proc/cpu********************"

cat /proc/cpuinfo

echo  "************/proc/meminfo*****************"

cat /proc/meminfo|grep MemTotal
 

echo  "******************************************"

echo  "             Hardware            "

echo  "******************************************"
 
 

echo  "************/etc/sysconfig/hwconf*********"

cat /etc/sysconfig/hwconf
 

#echo  "***********/sbin/sysctl -a****************"

#/sbin/sysctl -a
 

echo  "********/sbin/lspci***********************"

/sbin/lspci
 

echo  "******************************************"

echo  "             Boot Info           "

echo  "******************************************"
 

cat /etc/sysconfig/grub
 
 

echo  "******************************************"

echo  "             HBA driver          "

echo  "******************************************"

cd /proc/scsi; find . -type f -print | xargs egrep -i driver
 
 

echo  "******************************************"

echo  "             File System and Disks       "

echo  "******************************************"
 
 

#echo  "************df -k ************************"

#df -k >> /tmp/config.servername

echo  "**********/etc/fstab**********************"

cat /etc/fstab

echo  "**********mount***************************"

mount

echo  "*********fdisk -l*************************"

fdisk -l

echo  "********vgdisplay*************************"

vgdisplay

echo  "*********pvscan***************************"

pvscan

echo  "************lvmdiskscan*******************"

lvmdiskscan
 

echo  "******************************************"

echo  "             Net                 "

echo  "******************************************"
 

#echo  "***********ifconfig -a********************"

#ifconfig -a
 

#echo  "***********host***************************"

#host mozilla.org

echo  "***********ping test**********************"

#ping -c 4 -I eth0 -q www.mozilla.org|awk '{print $1,$2,$3,$4,$5}'|grep pack
 

echo  "******************************************"

echo  "               END OF REPORT               "

echo  "******************************************"

echo  "******************************************"

echo  "******************************************"
 
 

==================================STOP=============

Open in new window

0
Comment
Question by:bebo_c
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:glenthorne
Comment Utility
At first glance, one thing you might want to do is to separate your command by two ampersands (&&) instead of semicolons:

mount lucy:/usr/local/bin/UX_get_cfgs /mn && /mnt/sysconf.sh > /mnt/$HOST.before && umount /mnt

This will force each command to wait for the other to finish successfully.  You might be running into a situation where the final umount is occurring too soon.

Change $HOST to \$HOSTNAME - it worked on my machine.

Also, if the servers.txt contains only dogwood, does it run successfully?  I would verify that the scripts work on each host individually before throwing the entire list out.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 75 total points
Comment Utility

#!/bin/bash 
 

HOST=$(hostname)
 

while read server

do

  echo $server

  ssh root@$server "mount lucy:/usr/local/bin/UX_get_cfgs /mnt;/mnt/sysconf.sh >/mnt/$HOST.before;umount /mnt"

done < servers.txt

Open in new window

0
 

Author Comment

by:bebo_c
Comment Utility
I used some of both of your methods and the Servername works now but no loop:

==============START====================
#!/bin/sh
# get_before-cfgs.sh

set -x

while read server
do
echo $server
ssh root@$server "mount lucy:/usr/local/bin/UX_get_cfgs /mnt && /mnt/sysconf.sh > /mnt/$server.before && umount /mnt"
done < servers.txt
=====================STOP============================
=====================START===========================
#!/bin/sh
# get_after-cfgs.sh
set -x

while read server
do
echo $server
ssh root@$server "mount lucy:/usr/local/bin/UX_get_cfgs /mnt && /mnt/sysconf.sh > /mnt/$server.after && umount /mnt'
done < servers.txt

diff $server.before $server.after > $server.diff
=============================STOP========================

-rwx--x--x   1 root       sys            214 Jul 13 16:09
-rw-r--r--   1 root       root         44640 Jul 14 18:47 CYPRESS.after
-rw-r--r--   1 root       root         44640 Jul 14 18:42 CYPRESS.before
-rw-------   1 root       sys              0 Jul 14 18:47 CYPRESS.diff
-rwx--x--x   1 root       sys            272 Jul 14 18:47 get_after-cfgs.sh
-rwx--x--x   1 root       sys            232 Jul 14 18:41 get_before-cfgs.sh
-rwx------   1 root       sys            231 Jul 14 18:37 get_before-cfgs.sh.origg
-rwx--x--x   1 root       sys            256 Jul 14 18:32 servers.txt
-rwx--x--x   1 root       sys           2608 Jul 13 16:10 sysconf.sh


Creates proper filename...but does not loop through rest of servers in servers.txt. Is it because I am calling out another script ...

" /mnt/sysconf.sh > /mnt/$server.before"

Still not looping through the list. All servers are confirmed...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Assisted Solution

by:glenthorne
glenthorne earned 50 total points
Comment Utility
Just out of curiosity, in the script the server list is in the file "servers.txt", and in the original post, the file name has a capital "S" (Servers.txt).  Can you double check that?
0
 

Author Comment

by:bebo_c
Comment Utility
Yes,

It is servers.txt

====================start=====================

lucy_root> cat servers.txt
CYPRESS
DOGWOOD
FILBERT
HACKBERRY
WILLOW
CASCARA
CHERRY
CHESTNUT
CATALPA
CHINKAPIN
COTTONWOOD
BALSAM
BASSWOOD
ASPEN
BUCKEYE
BUCKTHORN
SEQUOIA
SPRUCE
SWEETGUM
SYCAMORE
TANOAK
LOCUST
MADRONE
POPLAR
REDCEDAR
REDWOOD
WALNUT
HAWTHORN
HAZEL
JUNIPER
HOLLY
LARCH
===============stop==============

lucy_root> ls -l
total 288
-rwx--x--x   1 root       sys            214 Jul 13 16:09
-rw-r--r--   1 root       root         44640 Jul 14 18:47 CYPRESS.after
-rw-r--r--   1 root       root         44640 Jul 14 18:42 CYPRESS.before
-rw-------   1 root       sys              0 Jul 14 18:47 CYPRESS.diff
-rwx--x--x   1 root       sys            257 Jul 14 18:49 get_after-cfgs.sh
-rwx--x--x   1 root       sys            232 Jul 14 18:41 get_before-cfgs.sh
-rwx------   1 root       sys            231 Jul 14 18:37 get_before-cfgs.sh.orig
-rwx--x--x   1 root       sys            256 Jul 14 18:32 servers.txt
-rwx--x--x   1 root       sys           2608 Jul 13 16:10 sysconf.sh
0
 

Author Comment

by:bebo_c
Comment Utility
Sorry, the script is here...........


==========start===========
#!/bin/sh
# get_before-cfgs.sh

set -x

HOST=$(hostname)

while read server
do
echo $server
ssh root@$server "mount lucy:/usr/local/bin/UX_get_cfgs /mnt && /mnt/sysconf.sh > /mnt/$server.before && u
mount /mnt"

done < servers.txt
=============stop=======================
0
 

Accepted Solution

by:
bebo_c earned 0 total points
Comment Utility
I changed the script and it works now:

lucy_root> cat cfg_before.sh
#!/bin/sh
# get_before-cfgs.sh

set -x

rm *.before *.after *.diff

ServNames="porthos cypress dogwood filbert hackberry willow cascara cherry chestnut catalpa chinkapin cottonwood sequoia spruce sycamore locust madrone poplar redwood walnut"

for Server in $ServNames

do
ssh  root@$Server "umount  /mnt"
ssh root@$Server "mount lucy:/usr/local/bin/UX_get_cfgs /mnt && /mnt/sysconf.sh > /mnt/$Server.before && umount /mnt"

done

0

Featured Post

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

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Utilizing an array to gracefully append to a list of EmailAddresses
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

11 Experts available now in Live!

Get 1:1 Help Now