The following code produces a 8192 byte file on machine2,
using both ruby 1.6.7 and 1.7.2.
I have tried just using the “system” commands by themselves
and they seem to work just fine. I have also tried removing
the “system” commands and the local file is not shortened
to 8192 bytes. So, something is getting corrupted in between.
I think it might be a file open/close issue, but since
this is my first “real” ruby program…I’m not having
luck figuring it out. If it matters the correct
file size is: 9579 bytes…at least with the current
iptables rules and subsitutions.
I thought it was my bad regexs…but thanks to Matz
and many others I got that part correct.
thanks as always,
todd
#!/usr/local/bin/ruby -w
this Ruby program takes the /etc/sysconfig/iptables from “machine1”
and creates an appropriate one for “machine2”
adds File.[“compare”, “move”, “safe_unlink”, “o_chmod”, “mv”,
“makedirs”, “cp”, “cmp”, “mkpath”, “syscopy”, “rm_f”,
“copy”, “install”, “catname”]
require ‘ftools’
···
in-line editting w/o a backup copy
$-i = “”
Change current working
Dir.chdir("/etc/sysconfig")
begin
copy iptables to iptables4machine2 and report any errors
File.copy(“iptables”,“iptables4machine2”,true)
rescue SystemCallError
$stderr.print "Copy failed: " + $! + "\n"
raise
end
Replace any command line arguments with iptables4machine2
begin
Replace any command line arguments with iptables4machine2
ARGV.replace([‘iptables4machine2’])
while line = gets
next if /^\s*#/ =~ line
next if /^\s*$/ =~ line
# perform substitution in place
# changes "machine1" external IP address "machine2"
line.gsub!(/10.1.0.1/,'10.1.0.2')
# changes "machine1" internal IP address "machine2"
line.gsub!(/192.168.1.1/,'192.168.1.2')
print line.gsub(/-A INPUT -s 192.168.1.2 -p icmp -m state --state NEW/,
'-A INPUT -s 192.168.1.1 -p icmp -m state --state NEW')
end
rescue SystemCallError
$stderr.print "Something failed: " + $! + "\n"
File.delete("iptables4machine2")
raise
end
system(“ssh machine2 cp /etc/sysconfig/iptables /etc/sysconfig/iptables.orig”)
system(“scp iptables4machine2 machine2:/etc/sysconfig/iptables”)
system(“ssh machine2 /etc/init.d/iptables stop”)
system(“ssh machine2 /etc/init.d/iptables start”)