Discussion:
[rancid] hostname glob - regex?
Paweł Rzepa
2014-02-06 07:44:01 UTC
Permalink
Hi,
When I log into my devices to get configuration with rancid I must use
two different users for two different groups of devices. I know that I
can modify .cloginrc to achieve this:

add user group-1-* {user1}
add user group-2-* {user2}

Unfortunately I am unable to describe groups of devices in such a
consistent manner and now my .cloginrc looks like this:

add user 192.168.1.* {user1}
add password 192.168.1.* {pass1}
add user 192.168.2.5 {user1}
add password 192.168.2.5 {pass1}
add user *-core-* {user1}
add password *-core-* {pass1}

add user 10.1.2.3 {user2}
add user 10.1.5.* {user2}
etc..

Is it possible to use regular expression to describe hostname? I'd
love to specify them like that:

add user {192.168.1.*|192.168.2.5|*-core-*} {user1}
add password {192.168.1.*|192.168.2.5|*-core-*} {pass1}

add user {10.1.2.3|10.1.5.*} {user2}
...

Best regards,
Pawel Rzepa
Paweł Rzepa
2014-02-09 23:01:34 UTC
Permalink
W dniu 06.02.2014 08:44, Pawel Rzepa pisze:

Hi,
Any help? I've digged into this a little and found that .cloginrc is
interpreted by expect. I'm not very familiar with expect, tried some
regexps with braces, escaped braces etc and no success. Is it impossible
to use regexp in this context?

Regards,
Pawel Rzepa
Post by Paweł Rzepa
Hi,
When I log into my devices to get configuration with rancid I must use
two different users for two different groups of devices. I know that I
add user group-1-* {user1}
add user group-2-* {user2}
Unfortunately I am unable to describe groups of devices in such a
add user 192.168.1.* {user1}
add password 192.168.1.* {pass1}
add user 192.168.2.5 {user1}
add password 192.168.2.5 {pass1}
add user *-core-* {user1}
add password *-core-* {pass1}
add user 10.1.2.3 {user2}
add user 10.1.5.* {user2}
etc..
Is it possible to use regular expression to describe hostname? I'd
add user {192.168.1.*|192.168.2.5|*-core-*} {user1}
add password {192.168.1.*|192.168.2.5|*-core-*} {pass1}
add user {10.1.2.3|10.1.5.*} {user2}
...
Best regards,
Pawel Rzepa
heasley
2014-02-10 17:30:57 UTC
Permalink
Post by Paweł Rzepa
Hi,
Any help? I've digged into this a little and found that .cloginrc is
interpreted by expect. I'm not very familiar with expect, tried some
regexps with braces, escaped braces etc and no success. Is it impossible
to use regexp in this context?
no; if it were, the manpage would have read regex, not glob. regex would
be useful; though i'm not sure how to implement it, since glob and regex
are not complatible. perhaps a global knob in cloginrc that switches the
format to regex? or an regex-specific version of the 'add' function and
search regexes before globs?

Per Olaf, ideas?
Post by Paweł Rzepa
Regards,
Pawel Rzepa
Post by Paweł Rzepa
Hi,
When I log into my devices to get configuration with rancid I must use
two different users for two different groups of devices. I know that I
add user group-1-* {user1}
add user group-2-* {user2}
Unfortunately I am unable to describe groups of devices in such a
add user 192.168.1.* {user1}
add password 192.168.1.* {pass1}
add user 192.168.2.5 {user1}
add password 192.168.2.5 {pass1}
add user *-core-* {user1}
add password *-core-* {pass1}
add user 10.1.2.3 {user2}
add user 10.1.5.* {user2}
etc..
Is it possible to use regular expression to describe hostname? I'd
add user {192.168.1.*|192.168.2.5|*-core-*} {user1}
add password {192.168.1.*|192.168.2.5|*-core-*} {pass1}
add user {10.1.2.3|10.1.5.*} {user2}
...
Best regards,
Pawel Rzepa
_______________________________________________
Rancid-discuss mailing list
http://www.shrubbery.net/mailman/listinfo/rancid-discuss
Per-Olof Olsson
2014-02-23 10:04:47 UTC
Permalink
Post by heasley
Post by Paweł Rzepa
Hi,
Any help? I've digged into this a little and found that .cloginrc is
interpreted by expect. I'm not very familiar with expect, tried some
regexps with braces, escaped braces etc and no success. Is it impossible
to use regexp in this context?
no; if it were, the manpage would have read regex, not glob. regex would
be useful; though i'm not sure how to implement it, since glob and regex
are not complatible. perhaps a global knob in cloginrc that switches the
format to regex? or an regex-specific version of the 'add' function and
search regexes before globs?
Per Olaf, ideas?
Just replace "string match" with "regexp" will make a big format change in cloginrc.

Think it's just to look for $ ( ) ^ | + or "\." ("([$)(^|+]|\\\.)+" in regexp)
to determining if it is regexp or not. Possible it will give some false match
and cloginrc need to be written more strict.

...
Post by heasley
Post by Paweł Rzepa
Post by Paweł Rzepa
add user {192.168.1.*|192.168.2.5|*-core-*} {user1}
add password {192.168.1.*|192.168.2.5|*-core-*} {pass1}
add user {10.1.2.3|10.1.5.*} {user2}
...

Just not that simple when you say regexp.
add user {^(192\.168\.1\..*|192\.168\.2\.5|.*-core-.*)$} {user1}

".*" for numbers? Why not "[0-9]+".
add user {^(192\.168\.1\.[0-9]+|192\.168\.2\.5|.*-core-.*)$} {user1}


Is this a possible way?


cloginrc in my test.
------cloginrc---------------------------------
add password {sw1.aaa.domain.se} x x
add method {10\.10\.1\.1|10\.10\.3\.[0-9]+|sw1\.aaa\.domain\.se} ssh
add password {10\.10\.1\.1|10\.10\.4\.[0-9]+} x x
add identity {^(10\.10\.1\.1|10\.10\.3\.[0-9]+|sw1\.aaa\.domain\.se)$} /home/rancid/.ssh/ssh_key
add autoenable {10\.10\.1\.1|10\.10\.3\.[0-9]+|sw1\.aaa\.domain\.se} 1

add password * y y
-----------------------------------------------


$ ./hlogin +debug_rc +debug_rc -c "show term" -f cloginrc -banner 10.10.3.74
10.10.3.74
Match regexp: 10.10.3.74 autoenable {10\.10\.1\.1|10\.10\.3\.[0-9]+|sw1\.aaa\.domain\.se} 1
No match : 10.10.3.74 password sw1.aaa.domain.se x x
No match : 10.10.3.74 password {10\.10\.1\.1|10\.10\.4\.[0-9]+} x x
Match : 10.10.3.74 password * y y
Match regexp: 10.10.3.74 identity {^(10\.10\.1\.1|10\.10\.3\.[0-9]+|sw1\.aaa\.domain\.se)$} /home/rancid/.ssh/ssh_key
Match regexp: 10.10.3.74 method {10\.10\.1\.1|10\.10\.3\.[0-9]+|sw1\.aaa\.domain\.se} ssh
spawn hpuifilter -- ssh -i /home/rancid/.ssh/ssh_key -c 3des -x -l rancid 10.10.3.74

switch.aaa.domain.s# no page
switch.aaa.domain.s# show term
Terminal width: 132 length: 24

switch.aaa.domain.s#logout
Do you want to log out [y/n]? y
Connection to 10.10.3.74 closed.



Have tested same update on clogin and hlogin 2.3.8
----------------------------------------
*** clogin 2014-02-21 11:03:17.903550570 +0100
--- clogin_new 2014-02-23 08:36:49.088227859 +0100
***************
*** 54,59 ****
--- 54,60 ----

# Usage line
set usage "Usage: $argv0 \[-dSV\] \[-autoenable\] \[-noenable\] \[-c command\] \
+ \[+debug_rc\] \
\[-Evar=x\] \[-e enable-password\] \[-f cloginrc-file\] \[-p user-password\] \
\[-r passphrase\] \[-s script-file\] \[-t timeout\] \[-u username\] \
\[-v vty-password\] \[-w enable-username\] \[-x command-file\] \
***************
*** 76,81 ****
--- 77,84 ----
# tracks if we receive them on the command line.
set do_passwd 1
set do_enapasswd 1
+ # debug matching cloginrc file
+ set debug_rc 0
# Save config, if prompted
set do_saveconfig 0
# Sometimes routers take awhile to answer (the default is 10 sec)
***************
*** 112,117 ****
--- 115,124 ----
# Expect debug mode
-d* {
exp_internal 1
+ # debug matching cloginrc file
+ # option twice, also show "No match"
+ } +debug_rc* {
+ set debug_rc [expr {$debug_rc + 1}]
# Username
} -u* {
if {! [regexp .\[uU\](.+) $arg ignore user]} {
***************
*** 282,292 ****

proc find {var router} {
upvar int_$var list
if { [info exists list] } {
foreach line $list {
! if { [string match [lindex $line 0] $router] } {
! return [lrange $line 1 end]
}
}
}
return {}
--- 289,311 ----

proc find {var router} {
upvar int_$var list
+ global debug_rc
+ set match_regexp 0
if { [info exists list] } {
foreach line $list {
! if { [ regexp {([$)(^|+]|\\\.)+} [lindex $line 0] ] } {
! catch { set match_regexp [regexp [lindex $line 0] $router] } reason
! if { $match_regexp != 0 } {
! if { $debug_rc } {send_user "Match regexp: $router $var $line\n"}
! return [lrange $line 1 end]
! }
! } else {
! if { [string match [lindex $line 0] $router] } {
! if { $debug_rc } { send_user "Match : $router $var $line\n" }
! return [lrange $line 1 end]
! }
}
+ if { $debug_rc >= 2 } {send_user "No match : $router $var $line\n"}
}
}
return {}
-----------------------------------------
Post by heasley
Post by Paweł Rzepa
Regards,
Pawel Rzepa
Post by Paweł Rzepa
Hi,
When I log into my devices to get configuration with rancid I must use
two different users for two different groups of devices. I know that I
add user group-1-* {user1}
add user group-2-* {user2}
Unfortunately I am unable to describe groups of devices in such a
add user 192.168.1.* {user1}
add password 192.168.1.* {pass1}
add user 192.168.2.5 {user1}
add password 192.168.2.5 {pass1}
add user *-core-* {user1}
add password *-core-* {pass1}
add user 10.1.2.3 {user2}
add user 10.1.5.* {user2}
etc..
Is it possible to use regular expression to describe hostname? I'd
add user {192.168.1.*|192.168.2.5|*-core-*} {user1}
add password {192.168.1.*|192.168.2.5|*-core-*} {pass1}
add user {10.1.2.3|10.1.5.*} {user2}
...
Best regards,
Pawel Rzepa
_______________________________________________
Rancid-discuss mailing list
http://www.shrubbery.net/mailman/listinfo/rancid-discuss
_______________________________________________
Rancid-discuss mailing list
http://www.shrubbery.net/mailman/listinfo/rancid-discuss
/Peo
----------------------------------------------------------
Per-Olof Olsson Email: ***@chalmers.se
Chalmers tekniska högskola IT-service
Arvid Hedvalls backe 6 412 96 Göteborg
Tel: 031/772 6738 Fax: 031/772 8680
----------------------------------------------------------
Alan McKinnon
2014-02-23 15:51:39 UTC
Permalink
Post by Per-Olof Olsson
Post by heasley
Post by Paweł Rzepa
Hi,
Any help? I've digged into this a little and found that .cloginrc is
interpreted by expect. I'm not very familiar with expect, tried some
regexps with braces, escaped braces etc and no success. Is it impossible
to use regexp in this context?
no; if it were, the manpage would have read regex, not glob. regex would
be useful; though i'm not sure how to implement it, since glob and regex
are not complatible. perhaps a global knob in cloginrc that switches the
format to regex? or an regex-specific version of the 'add' function and
search regexes before globs?
Per Olaf, ideas?
Just replace "string match" with "regexp" will make a big format change in cloginrc.
I would strongly recommend not implementing regexp in .cloginrc:

- regexps are heard to read and hard to debug
- users can apply them in a needlessly overcomplicated fashion
- when a regexp that describes a general case is early in the list and a
more specific match is later, the first match wins and this is really
really hard to debug, simply because there's no log to say which line in
.cloginrc was matched.

The principle use case for regexps over globs seems to be the |
operator, something like this:

add username 192.168.(0|1|2|3|4).* {username1}
add username 192.168.* {username2}

The first line can always be expanded to 5 individual lines with the
current syntax - less verbose but more explicit and fewer mistakes.
--
Alan McKinnon
***@gmail.com
Loading...