简体   繁体   English

BASH脚本 - 通过ssh连接并运行mysql命令

[英]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. 我正在尝试创建一个简单的bash脚本来ping许多客户端。 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: 如果客户端无法访问,那么它应该在另一台服务器上的数据库上启动更新...我对数据库服务器有无密码的ssh访问权限,所以我试图执行以下操作:

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 我收到一个bash: mysql: command not found错误使用它...显然在db服务器上安装了mysql

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

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. 由于您收到命令未找到错误,可能是在ssh上使用$PATH时出现问题。 Try connecting manually, and running which mysql to get the full path to the executable. 尝试手动连接,并运行which mysql以获取可执行文件的完整路径。 In my case, it's /usr/bin/mysql , then you might try using that in your statement, such as: 在我的例子中,它是/usr/bin/mysql ,那么您可以尝试在语句中使用它,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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