Solved

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

Posted on 2013-05-28
8
483 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ubuntu not booting - How get past GRUB? 3 84
How to use question mark (?) in filename with html 25 101
SSL/TLS - openssl troubleshooting 3 63
Redhat upgrade 1 48
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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