通过ssh传递外部的shell脚本变量 [英] Passing external shell script variable via ssh
问题描述
在我对面,我想从企业访问被封锁的一个邪恶的网站绊倒,编辑我绑定的服务器上我的named.conf文件中,然后更新我的代理服务器黑名单文件。我想用一个bash脚本有些自动执行此。说我的剧本被称为evil-site-block.sh,并包含以下内容:
SSH root@192.168.0.1回声#date增加$(日期+%M /%D /%Y)>> /var/named/chroot/etc/named.conf;回声带\\$ 1 \\{型主;文件\\的/ etc /区/虚拟块\\;}; >> /var/named/chroot/etc/named.conf
据然后运行为
$ evil-site-block.sh google.com
当我看着named.conf中的内容在远程机器上我看到:
#date加入2014年9月16日
区,{型主;文件在/ etc /区/虚拟块; };
我无法弄清楚是如何通过google.com为$ 1。
您的问题:你的整个命令被放入单引号 - 显然,这样的bash前pressions在服务器上展开,而不是在本地。
但是,这也适用于你的 $ 1
。
简单的解决方案:Interupt报价通过包装局部变量变成单引号
SSH root@192.168.0.1回声#date增加$(日期+%M /%D /%Y)>> /var/named/chroot/etc/named.conf;回声地带\\'$ 1 \\{型主;文件\\的/ etc /区/虚拟块\\;}; >> /var/named/chroot/etc/named.conf
注: \\$ 1 \\
→ \\'$ 1 \\
注:此解决方案是,张贴在这个问题上面只有一行简单的修补程序。如果这个脚本是由其他人执行,也可以处理任何类型的外部输出有丝毫的机会,请看看查尔斯达菲的解决方案。
When I stumble across an evil web site that I want blocked from corporate access, I edit my named.conf file on my bind server and then update my proxy server blacklist file. I'd like to automate this somewhat with a bash script. Say my script is called "evil-site-block.sh" and contains the following:
ssh root@192.168.0.1 'echo "#date added $(date +%m/%d/%Y)" >> /var/named/chroot/etc/named.conf; echo "zone \"$1\" { type master; file \"/etc/zone/dummy-block\"; };" >> /var/named/chroot/etc/named.conf'
It is then run as
$ evil-site-block.sh google.com
When I look at the contents of named.conf on the remote machine I see:
#date added 09/16/2014
zone "" { type master; file "/etc/zone/dummy-block"; };
What I can't figure out is how to pass "google.com" as $1.
Your problem: Your entire command is put into single quotes – obviously so that bash expressions are expanded on the server and not locally.
But this also applies to your $1
.
Simple solution: "Interupt" the quotation by wrapping your local variable into single quotes.
ssh root@192.168.0.1 'echo "#date added $(date +%m/%d/%Y)" >> /var/named/chroot/etc/named.conf; echo "zone \"'$1'\" { type master; file \"/etc/zone/dummy-block\"; };" >> /var/named/chroot/etc/named.conf'
NB: \"$1\"
→ \"'$1'\"
.
NOTE: This solution is a simple fix for the one-liner as posted in the question above. If there's the slightest chance that this script is executed by other people, or it could process external output of any kind, please have a look at Charles Duffy's solution.
这篇关于通过ssh传递外部的shell脚本变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!