Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 12-01-2017, 11:24 PM
jack_george jack_george is offline
Registered User
 
Join Date: Nov 2017
Posts: 11
Wink replace string in router config. using python

Hello
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:

Code:
@router> ...tch ARI |display set

#set policy-options policy-statement STM64-L1 term ARI from route-filter 1.1.1.1/32 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:

Code:
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")
crt.Screen.WaitForString("/n")
x=crt.Screen.ReadString("exact")
x.split(",")
term_name=x
term_name=list()
for word in term_name:
    h=term_name.index(pol)
    term_name.pop(h)
    term_name.insert(h,npt)
yyy= ' '.join(term_name)
crt.Screen.Send("edit private "+ "\n")
crt.Screen.WaitForString("router#")
crt.Screen.Send(yyy+ "\n")
crt.Screen.WaitForString("router#")
crt.Screen.Send("show | compare " + "\n")
crt.Screen.Synchronous = False
main()
but i face problem that script run without errors but no configuration applied like that value of yyy is just space ?

Thanks

Last edited by jdev; 12-05-2017 at 12:37 PM.
Reply With Quote
  #2  
Old 12-02-2017, 09:33 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,902
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?

Code:
crt.Dialog.MessageBox(term_name)
Also, I noticed in the 7th line of your code, you have a forward slash instead of backslash:

Code:
crt.Screen.WaitForString("/n")
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 12-05-2017, 12:31 AM
jack_george jack_george is offline
Registered User
 
Join Date: Nov 2017
Posts: 11
Smile replace sting using list

Hello,
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.


BR,
Jack
Reply With Quote
  #4  
Old 12-05-2017, 12:04 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,902
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:

Quote:
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):

Code:
#set policy-options policy-statement STM64-L1 term ARI from route-filter 1.1.1.1/32 exact
Then:
  • You cannot split on a comma because there is none in the output:
    Code:
    x.split(",")
  • This seems like it would be a simple replace operation:
Code:
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 12-05-2017, 11:51 PM
jack_george jack_george is offline
Registered User
 
Join Date: Nov 2017
Posts: 11
Smile 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

BR,
Reply With Quote
  #6  
Old 12-06-2017, 01:47 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,902
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply

Tags
python


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 01:50 PM.