VanDyke Software Forums

VanDyke Software Forums (
-   Scripting (
-   -   replace string in router config. using python (

jack_george 12-01-2017 11:24 PM

replace string in router config. using python
i try to run python script in securecrt for juniper routers such i show configuration on it then make change/replace for strings in it, i want to take output of show configuration then put it to list then make changes then change it to string then apply it.

example as below:


@router> ...tch ARI |display set

#set policy-options policy-statement STM64-L1 term ARI from route-filter exact
#set policy-options policy-statement STM64-L1 term ARI then accept
#set policy-options policy-statement STM64-L1 term rej then reject

in it i want to replace string STM64-L1 with 10G-L100 then apply configuration to router again as string

my code as below:


crt.Screen.Synchronous = True
JGR= crt.Dialog.Prompt("enter term name that will applied: ")   
rep= crt.Dialog.Prompt("enter term name that will removed ")   
pol= crt.Dialog.Prompt("enter policy that will remove term from it ")   
npt= crt.Dialog.Prompt("enter policy that will add term to it ")   
crt.Screen.Send("show configuration | display set | match " +JGR+ "\n")
for word in term_name:
yyy= ' '.join(term_name)
crt.Screen.Send("edit private "+ "\n")
crt.Screen.Send(yyy+ "\n")
crt.Screen.Send("show | compare " + "\n")
crt.Screen.Synchronous = False

but i face problem that script run without errors but no configuration applied like that value of yyy is just space ?


bgagnon 12-02-2017 09:33 AM

Hi jack_george,

I think you will need to sort of "walk back up" through your code, using Message Boxes to validate that your variables hold the data you expect (are not empty).

If you display term_name in a message box, is there data?


Also, I noticed in the 7th line of your code, you have a forward slash instead of backslash:


It was probably just a typo when composing the forum post and if your script completes, you must be getting the "new line" expected, but I thought I would check. :)

jack_george 12-05-2017 12:31 AM

replace sting using list
thanks you for reply , first regarding crt.Screen.WaitForString("/n") it was just type error when i post code while regarding using crt.Dialog.MessageBox(term_name) i can't pass list for method messagebox so instead i used crt.Dialog.MessageBox(yyy) and the output was just messagebox blank with symbol ! in it.


bgagnon 12-05-2017 12:04 PM

Hi Jack,

I am not really sure what you are trying to accomplish with this script. It seems you are making it more complicated than it needs to be.

If this is a true statement:


in it i want to replace string STM64-L1 with 10G-L100 then apply configuration to router again as string

And this is the output resulting from your Send() command (at least as much as you are capturing in the x variable):


#set policy-options policy-statement STM64-L1 term ARI from route-filter exact
  • You cannot split on a comma because there is none in the output:

  • This seems like it would be a simple replace operation:

strCommand = x.replace("STM64-L1", "10G-L100")

' Since commands don't typically begin with #, replace it also
strCommand = x.replace("#", "")

Additionally, you should be using \r (which simulates pressing Enter) not \n for the Send() commands.

jack_george 12-05-2017 11:51 PM

replace sting using list
Hello brenda
i know that i can use replace method instead but i want to accomplish this task using lists as per subject for other uses, regarding split i tried to split using x.split( ) but same problem output is blank
for using /n or /r i think it will not change the output as i work on linux env. thanks for for support


bgagnon 12-06-2017 01:47 PM

Hi Jack,

Then MessageBox "x" to determine what it holds prior to split. There's no magic answer here. You will just have to debug using the tools available. :)

All times are GMT -6. The time now is 12:32 PM.