简体   繁体   中英

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

你在单引号内有单引号,它应该是单个双引号:

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

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 '"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM