2015-01-15 56 views
1

我在我的bash脚本中运行sql时遇到了一些问题。有人可以建议什么语法需要改变?在sql字符串中使用bash参数的脚本问题

{ 
echo "listing" 
sshpass -p 'XXXXXX' ssh [email protected] 'mysql -h host -u user -pXXXXXX database -e "select user_id from users where concat(FIRST,LAST) like '%${username}%';"' 
} > $log 

以下是错误消息我收到:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%jaylefler%' at line 1 

当我更改了脚本推荐下面我收到以下错误:

bash: -c: line 0: syntax error near unexpected token `(' 
bash: -c: line 0: `mysql -h host-u user -pxxxxxxx database -e select user_id from users where concat(FIRST,LAST) like '%name_here%';' 

我原来的“工作段'如下:

echo "environment" 
sshpass -p $ldappw ssh [email protected] 'mysql -h host -u user -ppassword database -e "select concat(FIRST,LAST) from users;"' | (grep -i ${username} || echo "NO USER IDENTIFIED") 

我只是想修改这个,所以我可以打印出找到的用户标识,而不是每次找到姓和名时都打印出的用户名。

回答

2

你已经成熟了SQL注入攻击。

不过,sql需要单引号围绕模式。另外,你不需要引用你发送给ssh的命令。所以:

sshpass -p 'XXXXXX' ssh [email protected] mysql -h host -u user -pXXXXXX database -e "select user_id from users where concat(FIRST,LAST) like '%${username}%';" 
# ............................................^ remove quote ........................................................................................ remove quote^
+0

这是行不通的。 – user3299633

+0

“不起作用”比用作错误描述更糟糕。会发生什么? –

+0

更新原始帖子,遇到错误。 – user3299633