通过 ssh 传递外部 shell 脚本变量 [英] Passing external shell script variable via ssh

查看:56
本文介绍了通过 ssh 传递外部 shell 脚本变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我偶然发现一个我想阻止公司访问的邪恶网站时,我在绑定服务器上编辑了我的 named.conf 文件,然后更新了我的代理服务器黑名单文件.我想用一个 bash 脚本来自动化这个.假设我的脚本名为evil-site-block.sh"并包含以下内容:

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'

然后运行为

$ evil-site-block.sh google.com

当我在远程机器上查看named.conf的内容时,我看到:

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"; };

我不知道如何将google.com"作为 $1 传递.

What I can't figure out is how to pass "google.com" as $1.

推荐答案

你的问题:你的整个命令都放在单引号中——显然是为了 bash 表达式在服务器上而不是在本地展开.

Your problem: Your entire command is put into single quotes – obviously so that bash expressions are expanded on the server and not locally.

但这也适用于您的 $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'

注意:"$1""'$1'".

注意:此解决方案是对上述问题中发布的单行的简单修复.如果这个脚本有一点可能被其他人执行,或者它可以处理任何类型的外部输出,请查看 Charles达菲的解决方案.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆