Solved

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

Posted on 2013-05-28
8
477 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:atorex
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:atorex
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

728 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

14 Experts available now in Live!

Get 1:1 Help Now