Solved

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

Posted on 2013-05-28
8
478 Views
Last Modified: 2013-05-28
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
0
Comment
Question by:atorex
  • 5
  • 3
8 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39201293
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
0
 

Author Comment

by:atorex
ID: 39201401
That's what I was thinking but it errors out with

useradd: Too many arguments.
Try `useradd --help' or `useradd --usage' for more information.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39201407
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:atorex
ID: 39201488
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
0
 

Author Comment

by:atorex
ID: 39201491
if I pass
ssh $i useradd -d /home/kham -p salSp1wOPp6fk kham
this works fine!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39201529
This can only happen if the usernames in users.txt are not correctly separated.

Again:

What's exactly in users.txt?

cat -v users.txt
0
 

Author Comment

by:atorex
ID: 39201801
E420S aga # cat -v users.txt
kham
christab
kcash
rbryant
kdelley
janas
beths
charlie
sboyle
jcassel
vcoleman
vnajera
slozano
cshelton
0
 

Author Closing Comment

by:atorex
ID: 39201835
Sorry, I have found the issue, I had a missing s in $users.

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

Featured Post

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.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

823 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