ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So the script will ssh to any line with a #prod on on it - and (here is where the trouble is)
anything named capser or ghost i want to run one script, and anything else I want to run another.
having trouble though.
Code:
#!/bin/bash
for i in $(grep "#prod" /etc/hosts | awk '{print $2}')
do
ssh -q casper1@$i <<here
if [ hostname != "casper|ghost' ] ;
then
hostname
echo "/data/noc/crons/ping_gw_test_mcore.sh"
/data/noc/crons/ping_gw_test_mcore.sh
echo " "
else
hostname
echo "/data/noc/crons/ping_gw_test.sh"
/data/noc/crons/ping_gw_test.sh
echo " "
fi
here
done
is there something wrong with the if statment evaluation?
I am a little confused ... isn't the first column in the file the hostname? Why not test prior to ssh?
As for the test, my first would be to say using [[]] over [] can often help eliminate quirky errors, but as for the test itself I see a couple of issues:
1. Calling the command straight as you have the test is now comparing if the string 'hostname' is not equal to the other side (tip: think of how you normally get command output)
2. Is it likely that any of the hosts you are going to are called 'casper|ghost', ie the entire string including the pipe? I would guess not. maybe you are thinking of doing a regex match
which would then need to use =~
For a partial match of names, the case statement is best. It matches just like filenames are wildcarded on the command line (not regular expressions).
I didn't try to run it, but this should work:
Code:
#!/bin/bash
grep '#prod' /etc/host | while read IP NAME comment; do
case $NAME in
*casper|*ghost)
SCRIPT="/data/noc/crons/ping_gw_test.sh"
;;
*) SCRIPT="/data/noc/crons/ping_gw_test_mcore.sh"
;;
esac
ssh -q casper1@$IP "hostname; echo $SCRIPT; $SCRIPT"
echo
done
I have given up on making the <<here document work - for now.
This works - but only sends the script via ssh's to the first host in the list then exits.
Code:
#!/bin/bash
grep '#prod' /etc/hosts | while read IP Name comment;
do
case $NAME in
*casper|*ghost) SCRIPT="/data/noc/crons/ping_gw_test_mcore.sh" ;;
*)SCRIPT="/data/noc/crons/ping_gw_test.sh" ;;
esac
ssh -q casper@$IP "hostname; echo "$SCRIPT" ; $SCRIPT"
echo
done
Last edited by casperdaghost; 06-19-2012 at 08:21 AM.
This works - but only sends the script via ssh's to the first host in the list then exits.
The ssh commmand is reading stdin so it swallows all the data from grep, you can fix it by redirecting from /dev/null:
Code:
ssh -q casper@$IP "hostname; echo "$SCRIPT" ; $SCRIPT" < /dev/null
# or use -n option for ssh which does the same
ssh -n -q casper@$IP "hostname; echo "$SCRIPT" ; $SCRIPT"
Redirects stdin from /dev/null (actually, prevents reading from
stdin). This must be used when ssh is run in the background. A
common trick is to use this to run X11 programs on a remote
machine. For example, ssh -n shadows.cs.hut.fi emacs & will
start an emacs on shadows.cs.hut.fi, and the X11 connection will
be automatically forwarded over an encrypted channel. The ssh
program will be put in the background. (This does not work if
ssh needs to ask for a password or passphrase; see also the -f
option.)
the -n works great!!!!
and that solves alot of problems that i have been having scripting with ssh.
that is one of the reasons that i originally put this script in the here document.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.