Sam Smoot
7/6/2007 2:59:00 AM
On Jul 5, 1:39 pm, barjunk <barj...@attglobal.net> wrote:
> On Jul 3, 6:03 pm, Phrogz <g...@refinery.com> wrote:
>
>
>
> > On Jul 3, 6:54 pm, barjunk <barj...@attglobal.net> wrote:
>
> > > I'm trying to do validation on a firewall rule. The rule looks like
> > > this:
>
> > > -A eth0-IN -i eth0 -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j
> > > ACCEPT
>
> > > I know I can use split(' ') to break up the above entry into small
> > > parts, but what I really want is every other one.
>
> > > So '-A eth0' should be one then '-i eth0' should be the next....etc.
>
> > irb(main):001:0> s = "-A eth0-IN -i eth0 -s 192.168.0.0/24 -p tcp -m
> > tcp --dport 22 -j ACCEPT"
> > => "-A eth0-IN -i eth0 -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j
> > ACCEPT"
>
> > irb(main):002:0> s.scan /[^ ]+ [^ ]+/
> > => ["-A eth0-IN", "-i eth0", "-s 192.168.0.0/24", "-p tcp", "-m tcp",
> > "--dport 22", "-j ACCEPT"]
>
> I had to modify this just a bit to work with allowable multispace
> situations. An example is a rule that might look like this:
>
> -A eth0-IN -i eth0 -s 192.168.0.0/24 -p tcp -m tcp --dport
> 22 -j ACCEPT
>
> it looks like this: s.scan /[^ ]+ +[^ ]+/
>
> I suppose another solution would be to trim all the extra spaces first
> with some sort of gsub, then use the scan.
>
> Thanks again.
>
> Mike B.
Another nice way to do it would be to split it up into a Hash since
that would give you distinct option/value pairs.
# s = "-A eth0-IN -i eth0 -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -
j ACCEPT"
# => "-A eth0-IN -i eth0 -s 192.168.0.0/24 -p tcp -m tcp --dport 22 -j
ACCEPT"
# Hash[s.split]
# => {"-m"=>"tcp", "-p"=>"tcp", "-s"=>"192.168.0.0/24", "-i"=>"eth0",
"-j"=>"ACCEPT", "--dport"=>"22", "-A"=>"eth0-IN"}