Solved

read file script not working

Posted on 2009-07-13
7
245 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
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
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 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…

914 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

21 Experts available now in Live!

Get 1:1 Help Now