getfield¶
Description¶
A custom iRule function which splits a string on
a character or string, and returns the string corresponding to the
specific field.
Syntax¶
getfield <string> <split> <field_number>
getfield <string> <split> <field_number>
- Splits a string on a character or string, and returns the string corresponding to the specific field. The field_number parameter is 1 indexed.
Examples¶
Extract specific columns from a data group list match:
class HostRedirects {
www.domain.com https://www.domain.com/
host.domain.com http://host2.domain.com/
another.domain.com https://www.domain.com/another
}
rule HostRedirects {
when HTTP_REQUEST {
set row [findclass [HTTP::host] $::HostRedirects]
if { not ($row eq "")}{
HTTP::redirect [getfield $row " " 2][HTTP::uri]
return
}
}
}
To extract only the hostname from the host header (strips any trailing
“:###” port specification)
when HTTP_REQUEST {
set hostname [getfield [HTTP::host] ":" 1]
}
To redirect any request for a domain.com host to the same
hostname.subdomain @ domain.org (uses a multi-character split string
and field_number 1 to extract only those characters in the hostname
before the split string.):
when HTTP_REQUEST {
if { [HTTP::host] contains "domain.com"} {
HTTP::redirect https://[getfield [HTTP::host] ".domain.com" 1].domain.org[HTTP::uri]
}
}
- When used to compare a valid IP address (by Joe)
set addr "10.10.10.10"
set a [getfield $addr "." 1]
set b [getfield $addr "." 2]
set c [getfield $addr "." 3]
set d [getfield $addr "." 4]
# Or use scan instead to parse the IP octets to variables $a $b $c $d
scan $addr {%d.%d.%d.%d} a b d c
if { (0 <= $a) && ($a <= 255) &&
(0 <= $b) && ($b <= 255) &&
(0 <= $c) && ($c <= 255) &&
(0 <= $d) && ($d <= 255) } {
log local0. "$addr is a valid IP Address"
} else {
log local0. "$addr is NOT a valid IP Address"
}