besmart besmart - 2 years ago 129
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}
if ping -q -c 1 192.168.42.$i > /dev/null 2>&1
echo 1
ssh admin@ "mysql -u parkuser -ppass -e 'update SMARTPARK.client SET online=0 where SMARTPARK.ip_client=$ip'"


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

Answer Source

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@ "/usr/bin/mysql -u parkuser -ppass -e 'update SMARTPARK.client SET online=0 where SMARTPARK.ip_client=$ip '"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download