besmart besmart - 7 months ago 33
SQL Question

BASH Script - connect via ssh and run a mysql command

I'm trying to create a simple bash script that will ping a number of clients. If the client is not reachable, then it should launch an update on a db on another server... I have a passwordless ssh access to the db server, so i was trying to do the following:

for i in {11..25}
do
if ping -q -c 1 192.168.42.$i > /dev/null 2>&1
then
echo 1
else
ip=192.168.42.$i
ssh admin@192.168.3.240 "mysql -u parkuser -ppass -e 'update SMARTPARK.client SET online=0 where SMARTPARK.ip_client=$ip'"
fi

done


I'm getting a
bash: mysql: command not found
error when using it... Obviously mysql is installed on the db server

Answer

It seems to work fine for me. Since you're getting a command not found error, perhaps it is a problem with your $PATH on ssh. Try connecting manually, and running which mysql to get the full path to the executable. In my case, it's /usr/bin/mysql, then you might try using that in your statement, such as:

ssh admin@192.168.3.240 "/usr/bin/mysql -u parkuser -ppass -e 'update SMARTPARK.client SET online=0 where SMARTPARK.ip_client=$ip '"
Comments