我想执行以下shell脚本
system('echo "
rdr pass on lo0 inet proto tcp from any to 192.168.99.1 port 80 -> 192.168.99.1 port 8080
rdr pass on lo0 inet proto tcp from any to 192.168.99.1 port 443 -> 192.168.99.1 port 4443
" | sudo pfctl -ef - > /dev/null 2>&1; echo "==> Fowarding Ports: 80 -> 8080, 443 -> 4443 & Enabling pf"'
)
这很好用,我现在想传递从YAML文件加载的IP地址,我试过以下
config.yaml
configs:
use: 'home'
office:
public_ip: '192.168.99.2'
home:
public_ip: '192.168.99.1'
Vagrantfile
require 'yaml'
current_dir = File.dirname(File.expand_path(__FILE__))
configs = YAML.load_file("#{current_dir}/config.yaml")
vagrant_config = configs['configs'][configs['configs']['use']]
system('echo "
rdr pass on lo0 inet proto tcp from any to '+vagrant_config['public_ip']+' port 80 -> '+vagrant_config['public_ip']+' port 8080
rdr pass on lo0 inet proto tcp from any to '+vagrant_config['public_ip']+' port 443 -> '+vagrant_config['public_ip']+' port 4443
" | sudo pfctl -ef - > /dev/null 2>&1; echo "==> Fowarding Ports: 80 -> 8080, 443 -> 4443 & Enabling pf"'
)
第二种方法不起作用,也没有显示任何错误,有人能指出我正确的方向,我想要的是从配置文件或变量动态读取public_ip
由于
更新1
我得到以下输出
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
pfctl: pf already enabled
可能出现什么问题?
答案 0 :(得分:1)
出于疑难解答的目的,在将其发送到系统之前输出您要运行的命令是明智的。
cmd = 'echo "
rdr pass on lo0 inet proto tcp from any to '+vagrant_config['public_ip']+' port 80 -> '+vagrant_config['public_ip']+' port 8080
rdr pass on lo0 inet proto tcp from any to '+vagrant_config['public_ip']+' port 443 -> '+vagrant_config['public_ip']+' port 4443
" | sudo pfctl -ef - > /dev/null 2>&1; echo "==> Fowarding Ports: 80 -> 8080, 443 -> 4443 & Enabling pf"'
puts "Command to run:\n\n#{cmd}"
system( cmd )
然后,明智的做法是使系统命令的输出可见。为确保您收到此反馈,我建议您更换
sudo pfctl -ef - > /dev/null 2>&1
with(添加' -v'更详细的输出 - pfctl man page)
sudo pfctl -efv -
然后查找输出和/或错误消息。
然后,一旦错误被整理出来,你就可以把它恢复到隐蔽的安静模式:D
此外,由于您使用的是sudo
,因此您需要确保您在其中运行的shell具有sudo权限,并确保您不会被提示输入密码不知不觉。