Discussion:
[j-nsp] "replace pattern" Command & White Spaces
Mark Tinka
9 years ago
Permalink
Hi all.

We are trying to replace text in the configuration that contains spaces.

Documentation suggests this can be done by surrounding the source text
in quotes. But this does not work.

JTAC have no answer, which is odd.

Anyone know how to do this?

A "| match" works with quotes enclosed, but "replace pattern" does not.

All help appreciated.

Mark.
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Stacy W. Smith
9 years ago
Permalink
Can you show a specific example of the problem? What Junos release? What platform?

This appears to work for me on a vMX running 16.1R1.11:

[edit]
***@r0# replace pattern "foo bar" with "one two"

[edit]
***@r0# show | compare
[edit interfaces ge-0/0/0]
- description "foo bar";
+ description "one two";

--Stacy
Post by Mark Tinka
Hi all.
We are trying to replace text in the configuration that contains spaces.
Documentation suggests this can be done by surrounding the source text
in quotes. But this does not work.
JTAC have no answer, which is odd.
Anyone know how to do this?
A "| match" works with quotes enclosed, but "replace pattern" does not.
All help appreciated.
Mark.
_______________________________________________
https://puck.nether.net/mailman/listinfo/juniper-nsp
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Mark Tinka
9 years ago
Permalink
Post by Stacy W. Smith
Can you show a specific example of the problem? What Junos release? What platform?
[edit]
[edit]
[edit interfaces ge-0/0/0]
- description "foo bar";
+ description "one two";
Exactly the same thing as you're doing here, except I'm replacing "foo
bar" with "one".

I'm starting to suspect my Junos release - 14.2R6.5 on MX.

I do that and nothing happens.

Mark.
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Saku Ytti
9 years ago
Permalink
Hey,

I always just do:
start shell
zcat /config/juniper.conf.gz|sed 'magic' > new.conf
exit
configure exclusive
load override new.conf
commit and-quit


My main problem with 'replace' is, that it only considers 'user data',
it has no visibility on syntax, so you can't change ambiguous user
data, because you cannot add syntax to the match+replace.
Instead of trying to workaround with the implementation and what ever
corner cases I'm not yet aware of, I use generic tools I already know
how to use.

And obligatory configuration management note: I strongly believe only
correct way to deliver config to devices, is to deliver 100% from DB,
every time anything changes, and always only change is to replace all
of the config. This makes for very very simple provisioning system.
Unfortunately this configuration style excludes IOS, IOS-XE, TimOS and
many others, because they cannot replace whole config with new config,
which further means you'll need to add platform specific intelligence
in your configuration generation, which makes it expensive and hard to
generate config to such platforms.
Post by Mark Tinka
Hi all.
We are trying to replace text in the configuration that contains spaces.
Documentation suggests this can be done by surrounding the source text
in quotes. But this does not work.
JTAC have no answer, which is odd.
Anyone know how to do this?
A "| match" works with quotes enclosed, but "replace pattern" does not.
All help appreciated.
Mark.
_______________________________________________
https://puck.nether.net/mailman/listinfo/juniper-nsp
--
++ytti
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Mark Tinka
9 years ago
Permalink
Post by Saku Ytti
Hey,
start shell
zcat /config/juniper.conf.gz|sed 'magic' > new.conf
exit
configure exclusive
load override new.conf
commit and-quit
I considered using "sed" via the FreeBSD CLI, but trying to avoid
getting into the guts of the system.
Post by Saku Ytti
My main problem with 'replace' is, that it only considers 'user data',
it has no visibility on syntax, so you can't change ambiguous user
data, because you cannot add syntax to the match+replace.
Instead of trying to workaround with the implementation and what ever
corner cases I'm not yet aware of, I use generic tools I already know
how to use.
Not sure if I understand you - are you saying "replace" does not support
Junos commands, and only works on user configurations?

Mark.
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Saku Ytti
9 years ago
Permalink
Post by Mark Tinka
Not sure if I understand you - are you saying "replace" does not support
Junos commands, and only works on user configurations?
Consider you have metric 42, you want to change that to 43. Now you
probably don't want to risk changing every 42 => 43, because 42 could
be used in many context. So you'd maybe want to do replace "metric 42"
"metric 43", you can't, it won't have visibility on syntax/keyword
'metric', only on the variable/value '42'.
--
++ytti
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Mark Tinka
9 years ago
Permalink
Post by Saku Ytti
Consider you have metric 42, you want to change that to 43. Now you
probably don't want to risk changing every 42 => 43, because 42 could
be used in many context. So you'd maybe want to do replace "metric 42"
"metric 43", you can't, it won't have visibility on syntax/keyword
'metric', only on the variable/value '42'.
Got you.

Explains the issue I am seeing, then. I'm trying to replace "next term"
with "accept".

Mark.
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Patrick Okui
9 years ago
Permalink
Post by Saku Ytti
Unfortunately this configuration style excludes IOS, IOS-XE, TimOS and
many others, because they cannot replace whole config with new config,
which further means you'll need to add platform specific intelligence
in your configuration generation, which makes it expensive and hard to
generate config to such platforms.
Well you can kind of “copy tftp running-config” in may cases.

--
patrick
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://p
Saku Ytti
9 years ago
Permalink
Post by Patrick Okui
Well you can kind of “copy tftp running-config” in may cases.
That's merge, so any old stuff not needed anymore wouldn't be removed.
There is 'configure replace ...', but I'm not actually sure if it does
IOS-XR/JunOS style full replace/overwrite, without breaking stuff.
--
++ytti
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
h
Patrick Okui
9 years ago
Permalink
Post by Saku Ytti
Post by Patrick Okui
Well you can kind of “copy tftp running-config” in may cases.
That's merge, so any old stuff not needed anymore wouldn't be removed.
ack. That’s true.
Post by Saku Ytti
There is 'configure replace ...', but I'm not actually sure if it does
IOS-XR/JunOS style full replace/overwrite, without breaking stuff.
Hasn’t always worked for me (depends on platform). If I want to be
sure of overwrites then what you overwrite is the startup-config then
reload. Definitely not usable in most production environments.

--
patrick
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
Phil Shafer
9 years ago
Permalink
Post by Saku Ytti
Consider you have metric 42, you want to change that to 43. Now you
probably don't want to risk changing every 42 => 43, because 42 could
be used in many context. So you'd maybe want to do replace "metric 42"
"metric 43", you can't, it won't have visibility on syntax/keyword
'metric', only on the variable/value '42'.
Yup, it's field-oriented, since it's making changes in the database
directly; we're not generating text, pattern matching, and then
making change payloads. Instead we're traversing the database,
pattern matching each "leaf" in the database. So you get "^" and
"$", but not keywords like metric. It's definitely a trade-off,
but we've never really hidden the fact that we're not really keeping
the data in a straight text file.

FWIW, 'replace pattern "foo bar" with "moo"' work fine for me.

Thanks,
Phil
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Mark Tinka
9 years ago
Permalink
Post by Phil Shafer
Yup, it's field-oriented, since it's making changes in the database
directly; we're not generating text, pattern matching, and then
making change payloads. Instead we're traversing the database,
pattern matching each "leaf" in the database. So you get "^" and
"$", but not keywords like metric. It's definitely a trade-off,
but we've never really hidden the fact that we're not really keeping
the data in a straight text file.
Makes sense.

I'll work with "sed" in the FreeBSD CLI itself. A little more involved,
but will get the job done.

Would be good if JTAC got some training on this. It seems they don't
know that "replace" does not work on Junos syntax.

Mark.
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
9 years ago
Permalink
Post by Mark Tinka
I'll work with "sed" in the FreeBSD CLI itself. A little more involved,
but will get the job done.
You could use an op script, if you want to say completely in the
CLI. See attached.

Thanks,
Phil

--------
version 1.2;

var $conn = jcs:open();
param $pattern;
param $with;

var $pat = "(.*)" _ $pattern _ "(.*)";

main <op-script-results> {
var $rpc = <get-configuration format="text">;
var $res = jcs:execute($conn, $rpc);
var $new := {
for-each (jcs:break-lines($res)) {
var $re = jcs:regex($pat, .);
if ($re) {
expr $re[2] _ $with _ $re[3] _ "\n";
} else {
expr . _ "\n";
}
}
}
var $rpc2 = <load-configuration format="text"> {
<configuration-text> $new;
}
var $res2 = jcs:execute($conn, $rpc2);
copy-of $res2;
}
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

Continue reading on narkive:
Loading...