grouping and split

Discussion in 'Ruby' started by barjunk, Jul 4, 2007.

  1. barjunk

    barjunk Guest

    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.

    I guess I could use split then step through the resultant array and
    make another, but I thought there might be a better ruby style answer.

    Thanks for any guidance.

    Mike B.
     
    barjunk, Jul 4, 2007
    #1
    1. Advertising

  2. barjunk

    Phrogz Guest

    On Jul 3, 6:54 pm, barjunk <> 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"]
     
    Phrogz, Jul 4, 2007
    #2
    1. Advertising

  3. barjunk

    barjunk Guest

    On Jul 3, 6:03 pm, Phrogz <> wrote:
    > On Jul 3, 6:54 pm, barjunk <> 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"]


    Thanks tons for that...maybe it would be good practice to keep re-
    reading the chapter on the String class and a few other key ones, like
    maybe Enumerable?

    Any others folks would recommend?

    Mike B.
     
    barjunk, Jul 5, 2007
    #3
  4. barjunk

    barjunk Guest

    On Jul 3, 6:03 pm, Phrogz <> wrote:
    > On Jul 3, 6:54 pm, barjunk <> 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.
     
    barjunk, Jul 5, 2007
    #4
  5. barjunk

    Sam Smoot Guest

    On Jul 5, 1:39 pm, barjunk <> wrote:
    > On Jul 3, 6:03 pm, Phrogz <> wrote:
    >
    >
    >
    > > On Jul 3, 6:54 pm, barjunk <> 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"}
     
    Sam Smoot, Jul 6, 2007
    #5
  6. barjunk

    Chris Shea Guest

    On Jul 5, 8:59 pm, Sam Smoot <> wrote:
    >
    > 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"}


    Don't you need to splat the split?

    mvb:~ cms$ ruby -v
    ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
    mvb:~ cms$ irb
    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> Hash[s.split]
    ArgumentError: odd number of arguments for Hash
    from (irb):2:in `[]'
    from (irb):2
    from :0
    irb(main):003:0> Hash[*s.split]
    => {"-m"=>"tcp", "-p"=>"tcp", "-s"=>"192.168.0.0/24", "-i"=>"eth0", "-
    j"=>"ACCEPT", "--dport"=>"22", "-A"=>"eth0-IN"}
     
    Chris Shea, Jul 6, 2007
    #6
  7. barjunk

    Sam Smoot Guest

    On Jul 5, 10:28 pm, Chris Shea <> wrote:
    > On Jul 5, 8:59 pm, SamSmoot<> wrote:
    >
    >
    >
    > > 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"}

    >
    > Don't you need to splat the split?
    >
    > mvb:~ cms$ ruby -v
    > ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
    > mvb:~ cms$ irb
    > 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> Hash[s.split]
    > ArgumentError: odd number of arguments for Hash
    > from (irb):2:in `[]'
    > from (irb):2
    > from :0
    > irb(main):003:0> Hash[*s.split]
    > => {"-m"=>"tcp", "-p"=>"tcp", "-s"=>"192.168.0.0/24", "-i"=>"eth0", "-
    > j"=>"ACCEPT", "--dport"=>"22", "-A"=>"eth0-IN"}


    Yeah, I'm sorry. Got sloppy with my copy & paste and figured I could
    just write it faster I guess. :)
     
    Sam Smoot, Jul 6, 2007
    #7
  8. barjunk

    barjunk Guest

    On Jul 5, 7:42 pm, Sam Smoot <> wrote:
    > On Jul 5, 10:28 pm, Chris Shea <> wrote:
    >
    >
    >
    > > On Jul 5, 8:59 pm, SamSmoot<> wrote:

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

    >
    > > Don't you need to splat the split?

    >
    > > mvb:~ cms$ ruby -v
    > > ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
    > > mvb:~ cms$ irb
    > > 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> Hash[s.split]
    > > ArgumentError: odd number of arguments for Hash
    > > from (irb):2:in `[]'
    > > from (irb):2
    > > from :0
    > > irb(main):003:0> Hash[*s.split]
    > > => {"-m"=>"tcp", "-p"=>"tcp", "-s"=>"192.168.0.0/24", "-i"=>"eth0", "-
    > > j"=>"ACCEPT", "--dport"=>"22", "-A"=>"eth0-IN"}

    >
    > Yeah, I'm sorry. Got sloppy with my copy & paste and figured I could
    > just write it faster I guess. :)



    Although this doesn't account for a typo like leaving off some part of
    the pairs...could rescue for that situation.

    Thanks for all the feeback.
     
    barjunk, Jul 11, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    490
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    735
    Alex Martelli
    Sep 17, 2004
  3. Hvid Hat
    Replies:
    6
    Views:
    466
    Pavel Lepin
    Mar 7, 2008
  4. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    236
    Florian Gross
    Dec 28, 2004
  5. fishfry

    Choosing grouping of split items

    fishfry, Feb 15, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    172
    John W. Krahn
    Feb 16, 2004
Loading...

Share This Page