最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】mac中用sshpass通过-p传递密码包含特殊字符导致rsync报错:/bin/sh: -c: line 0: syntax error near unexpected token `)’

Mac crifan 4852浏览 0评论

折腾:

【基本解决】Makefile中从独立文件比如json中读取配置变量

期间,mac中,写makefile去自动调用rsync上传文件。

其中之前已通过sshpass加上密码文件传递密码,是没问题的。

此处整合了配置后,把密码放到另外makefile中,然后变成变量,再通过sshpass的-p参数传递到rsync:

  sshpass -p $(DEPLOY_SERVER_PASSWORD) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH)

结果报错了:

sshpass -p xxx)xxx rsync -avzh --progress --stats --delete --force /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/selenium_summary/output/selenium_summary [email protected]:/data/wwwroot/book.crifan.com/books
/bin/sh: -c: line 0: syntax error near unexpected token `)'
/bin/sh: -c: line 0: `sshpass -p xxx)xxx rsync -avzh --progress --stats --delete --force /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/selenium_summary/output/selenium_summary [email protected]:/data/wwwroot/book.crifan.com/books'
make: *** [upload] Error 2

即:

此处密码包含特殊的右括号,导致命令无法执行

自己手动给密码加上双引号,也不行:

➜  selenium_summary git:(master) ✗ sshpass -p "xxx)xxx" rsync -avzh --progress --stats --delete --force /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/selenium_summary/output/selenium_summary [email protected]:/data/wwwroot/book.crifan.com/books
Permission denied, please try again.
Permission denied, please try again.
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(700) [sender=3.1.3]

会由于密码错误而被拒绝

后记:

后来发现此处密码搞错了,变短了,原因是:

由另外makefile中传入的密码,在右括号后面还有#,结果被当做注释,忽略掉了。。。

所以问题转换为:

如何确保sshpass的password中包含特殊字符

sshpass password special characters

Bash script and escaping special characters in password – Ask Ubuntu

command line – Permission denied when trying to run a script that uses ‘sshpass’ – Ask Ubuntu

Revision – Ask Ubuntu

command line – Shell script password with special chars – Ask Ubuntu

python – Handling special characters in password field of sshpass – Stack Overflow

用反斜杠转义

shell – Bash script and escaping special characters in password – Stack Overflow

linux – Get and use a password with special characters in Bash shell – Stack Overflow

linux – how to escape the single quotes when use sshpass – Stack Overflow

shell – bash: how to pass password containing special characters – Stack Overflow

是说给参数变量加上双引号

去试试

sshpass -p "$(DEPLOY_SERVER_PASSWORD)" rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(DEPLOY_SERVER_USER)@$(DEPLOY_SERVER_IP):$(DEPLOY_SERVER_PATH)

结果:

sshpass -p "xxx)xxx" rsync -avzh --progress --stats --delete --force /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/selenium_summary/output/selenium_summary [email protected]:/data/wwwroot/book.crifan.com/books
Permission denied, please try again.
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(700) [sender=3.1.3]
make: *** [upload] Error 5

问题依旧。

补充一句:

我之前就是用

sshpass -f file_name

此处不希望用另外独立文件存储密码,所以不想用-f。

自己手动给密码中个数字符加上反斜杠去转义

DEPLOY_SERVER_PASSWORD=xxx\)xxx\#\?xxx

然后是可以的:

sshpass -p xxx\)xxx\#\?xxx rsync -avzh --progress --stats --delete --force /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/selenium_summary/output/selenium_summary [email protected]:/data/wwwroot/book.crifan.com/books
sending incremental file list
...
sent 324.85K bytes  received 67.09K bytes  12.06K bytes/sec
total size is 16.14M  speedup is 41.18

【总结】

此处makefile:

GitbookCommon.mk

中导入了另外一个makefile:

DeployServerInfo.mk

其中包含密码配置:

DEPLOY_SERVER_PASSWORD=xxx)xxx#?xxx

导入后,就由于#被当做注释,已经变成了:

DEPLOY_SERVER_PASSWORD=xxx)xxx

了,然后肯定会错误。

另外又由于此处

sshpass -p xxx)xxx rsync -avzh ...

也会由于密码中包含右括号这个特殊字符而报错

原因:sshpass -p传入的密码不能包含特殊字符

解决办法:

把特殊字符用反斜杠转义

注:此处密码中特殊字符有:

  • 右括号

  • 井号

  • 问号

去把

DeployServerInfo.mk

改为:

DEPLOY_SERVER_PASSWORD=xxx\)xxx\#\?xxx

结果导入后,就能识别完整长度的密码,且密码也可以用-p参数传入了:

sshpass -p xxx\)xxx\#\?xxx rsync -avzh ...

能正常工作了。

转载请注明:在路上 » 【已解决】mac中用sshpass通过-p传递密码包含特殊字符导致rsync报错:/bin/sh: -c: line 0: syntax error near unexpected token `)’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.174 seconds, using 22.09MB memory