Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

read file script not working

Posted on 2009-07-13
7
Medium Priority
?
267 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 300 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 3

Assisted Solution

by:glenthorne
glenthorne earned 200 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
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…
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.:
Suggested Courses

715 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