Network Change & Configuration Management

Reply
Highlighted

Need to manipulate variables within a CCS script -- How do I do this?

TNicoll
Techie
Posts: 1
4566     0

I was attempting to write a script that would pull the IP address of a device from the configuration, and manipulate the individual octet values and use the manipulated values to modify the configuration.  After speaking to Sif (thanks Sif!), he mentioned that "expr" is how this is done.  It took a bit of playing around, but I was able to come up with a valid script as follows:

 

Script-Filter:
    $Vendor eq "Cisco"     

#########################################################################
Action:
    Modify ACL to allow SSH

Action-Description:
    Modify ACL to allow SSH

Action-Commands:
    show running-config interface vlan345 | include ip address

Output-Triggers:
    Create access-list

#########################################################################
Trigger:
    Create access-list
Trigger-Description:
    Configure ACL 5 and other parameters
Trigger-Variables:
    $VLAN345 integer
    $VLAN346 integer
    $OCT3 integer
    $OCT4NET integer
    $OCT4FIRST integer
    $OCT4SECOND integer
Trigger-Template:
    ip address 10\.[[$VLAN345]]\.[[$OCT3]]\.[[$OCT4FIRST]] 255.255.255.240
Trigger-Commands:
    expr: $VLAN346 = $VLAN345 - 16
    expr: $OCT4NET = $OCT4FIRST - 1
    expr: $OCT4SECOND = $OCT4FIRST + 1
    conf t
    line vty 0 4
     no access-class 10 in vrf-also
    exit
    no access-list 10
    access-list 10 permit 10.$VLAN345.$OCT3.$OCT4NET 0.0.0.15
    access-list 10 permit 10.$VLAN346.$OCT3.$OCT4SECOND
    line vty 0 4
     access-class 10 in vrf-also
    end
    wr

#########################################################################

 

 

Thanks again for the help, Sif!

Unfortunately, you can't.  I

Guru
Posts: 61
4567     0

Unfortunately, you can't.  I've beaten my head against this a few times now... spent most of yesterday experimenting with this specifically to no avail.

 

With the exception of filters, variable type definitions are meaningless in commands and they are all treated as strings.

You will want to use Perl instead of CCS, if you need a number to behave as a number.

Daniel,

Adviser
Posts: 426
4567     0

Daniel,

The script above works, let's setup a call next week, when I'm working in the home office.

You have to make sure you capture it as an integer and not a string.  I helped Tim work this script out.

Thanks,

Sif

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

A call sounds good.

Guru
Posts: 61
4567     0

A call sounds good.

Does the complexity of the math, or the position of the variable in the string make a difference?

Trigger-Variables:

$str /\d{4}/ number   # i also tried '$str int'

Trigger-Commands:

EXPR: $second = (10000+$str)/64
EXPR: $third = (((10000+$str) %64) *4)+1

I then tried to use them in an action with-

ip address 10.$second.$third.252 255.255.255.0

but it would fail with invalid input because the result is never calculated.

*** Invalid input detected ***

 Switch(config-if)#ip address 10.(10000+4101)/64..253 255.255.255.0 
                                    ^ 
 % Invalid input detected at '^' marker. 

Try the following:

Adviser
Posts: 426
4567     0

Try the following:

For

EXPR: $second = (10000+$str)/64

Do:

EXPR: $second = 10000+$str

EXPR: $second = $second/64

I do belive you have to break each one out:
EXPR: $third = (((10000+$str) %64) *4)+1

EXPR: $third = 10000+$str

EXPR: $third = $third%64

EXPR: $third = $third*4

EXPR: $third = $third+1

 

Let me know i that works for you

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Still getting the same

Guru
Posts: 61
4567     0

Still getting the same results - tried a couple variations

 

15:44:00   Trigger-Commands 
 15:44:00     EXPR: $base = $str+10000 
 15:44:00     EXPR: $second = $base/64 
 15:44:00     EXPR: $third = $base%64 
 15:44:00     EXPR: $third = $third*4 
 15:44:00   

 EXPR: $third = $third+1 

 

 

 15:44:00   
 ip address 10.0003+10000/64.0003+10000%64*4+1.252 255.255.255.0 
 15:44:00   
 standby 61 ip 10.0003+10000/64.0003+10000%64*4+1.254 

 

Found something when I looked

Adviser
Posts: 426
4567     0

Found something when I looked at the man pages for expr on my linux box, you need spaces beween the symblos

 22:33:41   
 logging (192).168.1.11 
 22:33:41   Trigger-Variables 
 22:33:41     $test = '192' 
 22:33:41   Trigger-Commands 
 22:33:41     EXPR: $base = $test + 10000 
 22:33:41   
10192 

So you just need to do the following:

EXPR: $base = $test + 10000    - notice the space between $test and + and then between + 10000

Let me know if it works for you, I'm on version 6.8.6 

 

Sif

 

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

I also checked you command

Adviser
Posts: 426
4567     0

I also checked you command and it worked as well with spaces

 06:34:38     Template matches 
 06:34:38   
 logging (192).168.1.11 
 06:34:38   Trigger-Variables 
 06:34:38     $test = '192' 
 06:34:38   Trigger-Commands 
 06:34:38     EXPR: $base = $test+10000 
 06:34:38     EXPR: $second = ( 10000 + $test ) / 64 
 06:34:38   
192+10000 
 06:34:38   
159 
Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Thanks Sif!  That worked.

Guru
Posts: 61
4567     0

Thanks Sif!  That worked. 

In this specific case, i ended up writing the script for Perl because I needed it this week, but this will be very handy for future scripts.

Re: Need to manipulate variables within a CCS script -- How do I do this?

animeshsaxena
Techie
Posts: 1
4567     0

Is this code still working?

Re: Need to manipulate variables within a CCS script -- How do I do this?

Adviser
Posts: 426
4567     0

Why don't you start by creating a new posting linking to this and an example of what you are trying to do Smiley Happy

 

Or the script, we will help

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh
Showing results for 
Search instead for 
Do you mean 

Recommended for You