Solved

read file script not working

Posted on 2009-07-13
7
254 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
[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
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:glenthorne
ID: 24845168
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
ID: 24845509

#!/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
ID: 24855666
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 3

Assisted Solution

by:glenthorne
glenthorne earned 50 total points
ID: 24859238
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
ID: 24859976
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
ID: 24859996
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
ID: 24861783
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
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.

763 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