[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ssh inside a loop problem
From: |
Bob Proulx |
Subject: |
Re: ssh inside a loop problem |
Date: |
Fri, 5 Oct 2007 12:37:29 -0600 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
caudio wrote:
> Hi everybody! I have recently had a problem trying to make an ssh
> runn inside a loop.
Your question is really not about a bug in bash. It would be more
appropriate for a general shell script discussion list. It is
somewhat off-topic here.
Having said that, I can't resist commenting regardless.
> for s in $(cat $SERVER_FILE);
Search the web for "useless use of cat".
> do
> ssh $s "
Because your argument is a double-quoted string all $var variables
will be expanded on the local machine and not on the remote machine.
> PATH=$PATH:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/lbin:/usr/local/etc;
>
This will be the $PATH on the local machine because of this.
> for i in $(cat $USERS_FILE);
This will be the $USERS_FILE on the local machine, which is what you
want, I think.
> do
> echo $i >> $HOME/line ||touch $HOME/line
This will be the $i on the local machine, which is *not* what you
want. You will need to quote the $ there so that it gets expanded on
the remote machine.
And so on.
> line=$(sed -e 's/_/ /g' $HOME/line)
> NetworkId=$(echo "$line");
> rm -r $HOME/line
Is the -r needed there? Safer without it.
I think there is a lot of unnecessary work being done there but I
don't have the brain cells at the moment to analyze it further and
will simply note that this bothers me.
> sudo userdel -r $NetworkId
> echo "All the task on Server:$s has been completed :D"
Where is 's' set?
> done
>
> exit "
> done
The technique I like better is to cat the script into the remote
machine. Consider something like this:
s=somethinghere
ssh host.example.com sh <<EOF
PATH=\$PATH:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/lbin:/usr/local/etc;
for i in $(<$USERS_FILE); do
echo \$i >> \$HOME/line || touch \$HOME/line
line=\$(sed -e 's/_/ /g' \$HOME/line)
NetworkId=\$(echo "$line");
rm \$HOME/line
sudo userdel -r \$NetworkId
echo "All the task on Server:$s has been completed :D"
done
exit 0
EOF
Or like this:
PATH=$PATH:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/lbin:/usr/local/etc;
for i in $(<$USERS_FILE); do
echo $i >> $HOME/line || touch $HOME/line
line=$(sed -e 's/_/ /g' $HOME/line)
NetworkId=$(echo "$line");
rm $HOME/line
ssh host.example.com sh <<EOF
sudo userdel -r $NetworkId
EOF
echo "All the task on Server:$s has been completed :D"
done
exit 0
I did not test any of this.
I assume by your use here that you have sudo configured to not ask for
a password? Otherwise this will need adjustment.
Good luck!
Bob