Link to home
Start Free TrialLog in
Avatar of atorex
atorex

asked on

Bash script looping two source files in a (for loop)

I need some help adding two source files to a for loop, below is what I have, essentially I'm using my hosts file to source the IP I need to connect to, I than have a file with a list of users I need to add in each of the nodes on the hosts file.


# Obtain IP from hosts file
NODES=`cat /etc/hosts | grep S****$1 |cut -f2 -d ' '`
USERS=`cat users.txt`
ERRLOG=send_USERS_err.log
RUNLOG=send_UDERS_run.log


#Perform a ping test to each IP validating system is up

for i in $NODES
do
      ping -c 1 $i >> /dev/null
if [ $? != 0 ]; then
      echo " $i is currently DOWN " >> $ERRLOG
else
ssh $i useradd -d /home/$USER -p salSp1wOPp6fk $USER
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Like this?

NODES=`cat /etc/hosts | grep S****$1 |cut -f2 -d ' '`
USERS=`cat users.txt`
ERRLOG=send_USERS_err.log

for i in $NODES
do
      ping -c 1 $i >> /dev/null
if [ $? -ne 0 ]; then
      echo " $i is currently DOWN " >> $ERRLOG
else
for USER in $USERS
  do
ssh $i useradd -d /home/$USER -p salSp1wOPp6fk $USER
  done
fi
done
Avatar of atorex
atorex

ASKER

That's what I was thinking but it errors out with

useradd: Too many arguments.
Try `useradd --help' or `useradd --usage' for more information.
What's in "users.txt"?

Please note: it's

for USER in $USERS

not

for USER in "$USERS"

You can add

set -xv

just above the "ssh $i ..." line, to see what happens.
Avatar of atorex

ASKER

yup I have for USER in $USERS

what its doing is adding all users in the file in one command see below

+ set -xv
+ ssh 172.28.78.186 useradd -d /home/kham christab kcash rbryant kdelley janas beths charlie sboyle jcassel vcoleman vnajera slozano cshelton -p salSp1wOPp6fk kham christab kcash rbryant kdelley janas beths charlie sboyle jcassel vcoleman vnajera slozano cshelton
Avatar of atorex

ASKER

if I pass
ssh $i useradd -d /home/kham -p salSp1wOPp6fk kham
this works fine!
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of atorex

ASKER

E420S aga # cat -v users.txt
kham
christab
kcash
rbryant
kdelley
janas
beths
charlie
sboyle
jcassel
vcoleman
vnajera
slozano
cshelton
Avatar of atorex

ASKER

Sorry, I have found the issue, I had a missing s in $users.

what you provide works perfectly my apologies for the typo!!