Forum Discussion

soymanue's avatar
soymanue
Icon for Nimbostratus rankNimbostratus
Oct 12, 2012

Hostname split

Hello

 

I need to split a hostname into three parts ( ..).

 

This can be done al least on two ways:

 

1) scan [HTTP::host] %\[^.\].%\[^.\].%s prefix host suffix

 

 

2) set prefix [getfield [HTTP::host] "." 1]

 

set host [getfield [HTTP::host] "." 2]

 

set suffix [getfield [HTTP::host] "." 3]

 

 

Both work, but the problem comes when there is no prefix ( f5.com instead of www.f5.com). In that case scan fails and the second method leaves suffix empy, assign .com to host and f5 to prefix

 

I've done this to solve this:

 

set longitud [ string length $suffix ]

 

if { $longitud < 1 } {

 

set host [string tolower $prefix ]

 

}

 

It works, but it doesn't look quite ellegant nor efficient.

 

 

Is there a better way to solve that particular case?

 

Thank you.

 

9 Replies

  • this is mine but i think it is not efficient anyway. 🙂

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when RULE_INIT {
       set hostname "f5.com"
       set last_dot [string last "." $hostname]
       set second_dot [string last "." $hostname [expr {$last_dot -1}]]
       set suffix [string range $hostname [expr {$last_dot + 1}] end]
       set host [string range $hostname [expr {$second_dot + 1}] [expr {$last_dot - 1}]]
       set prefix [string range $hostname 0 [expr {$second_dot - 1}]]
       log local0. "hostname=$hostname | prefix=$prefix | host=$host | suffix=$suffix"
    
       set hostname "www.f5.com"
       set last_dot [string last "." $hostname]
       set second_dot [string last "." $hostname [expr {$last_dot -1}]]
       set suffix [string range $hostname [expr {$last_dot + 1}] end]
       set host [string range $hostname [expr {$second_dot + 1}] [expr {$last_dot - 1}]]
       set prefix [string range $hostname 0 [expr {$second_dot - 1}]]
       log local0. "hostname=$hostname | prefix=$prefix | host=$host | suffix=$suffix"
    
       set hostname "xxx.yyy.f5.com"
       set last_dot [string last "." $hostname]
       set second_dot [string last "." $hostname [expr {$last_dot -1}]]
       set suffix [string range $hostname [expr {$last_dot + 1}] end]
       set host [string range $hostname [expr {$second_dot + 1}] [expr {$last_dot - 1}]]
       set prefix [string range $hostname 0 [expr {$second_dot - 1}]]
       log local0. "hostname=$hostname | prefix=$prefix | host=$host | suffix=$suffix"
    }
    }
    
    [root@ve10:Active] config  tail -f /var/log/ltm
    Oct 12 19:25:48 local/ve10 err mcpd[3815]: 01020066:3: The requested rule (myrule) already exists in partition Common.
    Oct 12 19:25:48 local/tmm info tmm[7926]: Rule myrule : hostname=f5.com | prefix= | host=f5 | suffix=com
    Oct 12 19:25:48 local/tmm info tmm[7926]: Rule myrule : hostname=www.f5.com | prefix=www | host=f5 | suffix=com
    Oct 12 19:25:48 local/tmm info tmm[7926]: Rule myrule : hostname=xxx.yyy.f5.com | prefix=xxx.yyy | host=f5 | suffix=com
    
  • I wouldn't know how to code it but assuming you know that the domain name or names will always end .com or whatever, why not just compare $host against .com and if it's a match, shuffle your fields: set suffix = $host, set host = $prefix, set prefix = "blank"?

     

  • We have several suffixes: .com, .org, .es, .biz, .eu, .int

     

     

  • I don't how how does it work

     

    Where can I find and example?

     

    Thanks
  • I'd probably turn it into a list:

    
    set myhost [split [HTTP::host] "."]
    

    Then you could use the full force of list commands for processing.

    - llength to determine the size of the hostname

    - the last index is the root: [expr [llength $mylist] - 1]
  • Kevin's probably onto something there but if you did take the data group route, create and populate the data group and use code something like this;

     

     

    
    if { [class match [string tolower $host] contains suffix_data_group ] } {
                'do something here'
            }
    
  • i like Kevin's idea.

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when RULE_INIT {
       set hostname "f5.com"
       set lhostname [split $hostname "."]
       set suffix [lindex $lhostname [expr {[llength $lhostname] - 1}]]
       set host [lindex $lhostname [expr {[llength $lhostname] - 2}]]
       set prefix [lindex $lhostname [expr {[llength $lhostname] - 3}]]
       for {set i [expr {[llength $lhostname] - 4}]} {$i >= 0} {incr i -1} {
          set prefix "[lindex $lhostname $i].$prefix"
       }
       log local0. "hostname=$hostname | prefix=$prefix | host=$host | suffix=$suffix"
    
       set hostname "www.f5.com"
       set lhostname [split $hostname "."]
       set suffix [lindex $lhostname [expr {[llength $lhostname] - 1}]]
       set host [lindex $lhostname [expr {[llength $lhostname] - 2}]]
       set prefix [lindex $lhostname [expr {[llength $lhostname] - 3}]]
       for {set i [expr {[llength $lhostname] - 4}]} {$i >= 0} {incr i -1} {
          set prefix "[lindex $lhostname $i].$prefix"
       }
       log local0. "hostname=$hostname | prefix=$prefix | host=$host | suffix=$suffix"
    
       set hostname "xxx.yyy.f5.com"
       set lhostname [split $hostname "."]
       set suffix [lindex $lhostname [expr {[llength $lhostname] - 1}]]
       set host [lindex $lhostname [expr {[llength $lhostname] - 2}]]
       set prefix [lindex $lhostname [expr {[llength $lhostname] - 3}]]
       for {set i [expr {[llength $lhostname] - 4}]} {$i >= 0} {incr i -1} {
          set prefix "[lindex $lhostname $i].$prefix"
       }
       log local0. "hostname=$hostname | prefix=$prefix | host=$host | suffix=$suffix"
    }
    }
    
    [root@ve10:Active] config  tail -f /var/log/ltm
    Oct 12 19:58:52 local/ve10 err mcpd[3815]: 01020066:3: The requested rule (myrule) already exists in partition Common.
    Oct 12 19:58:52 local/tmm info tmm[7926]: Rule myrule : hostname=f5.com | prefix= | host=f5 | suffix=com
    Oct 12 19:58:52 local/tmm info tmm[7926]: Rule myrule : hostname=www.f5.com | prefix=www | host=f5 | suffix=com
    Oct 12 19:58:52 local/tmm info tmm[7926]: Rule myrule : hostname=xxx.yyy.f5.com | prefix=xxx.yyy | host=f5 | suffix=com