Jump to content

Template


Sypher
 Share

Recommended Posts

Hello,

I'm working on a tool that can load a template and replace "some" variables in that file.

There are a LOT of them, around 80 i guess.

All of them are built like {name}.

Well the problem is how can i automate it?

Here's a part of the template-file:

CODE

ip domain-name {DOMAIN}

ip name-server {NS1}

ip name-server {NS2}

<...snip...>

interface FastEthernet0/9

{FE9}

!

interface FastEthernet0/10

{FE10}

!

interface FastEthernet0/11

{FE11}

!

interface FastEthernet0/12

{FE12}

How can i do this the most efficient way, because 80 times:

if StringInStr($tplarray[$x], "{VAR}",0) Then
        $newvar = StringReplace($tplarray[$x], "{VAR}", $var, 2, 0)
    EndIf

Is a bit too much i guess :)

Afterwards the file should be saved to a new filename, the template is only a... template

I hope you can help me out. Thanks in advance!

Edited by Sypher
Link to comment
Share on other sites

Create an array with template and corresponding values

$temp[2,2] = [["{DOMAIN}","my.domain.com"],["{NS1}", "dns.dyn.com"],["{NS2}","dns.domain.com"], ["{FE9}","10.0.0.3"]]oÝ÷ ÙvØ^~)^{hjv«­¬ºÚ"µÍÌÍÙ]ÈHÑ[UØ^J   ÌÍÜ][YJoÝ÷ ØKÚæ­yæX§z+^¨{-jYmëÚç¬{a{Múµé©jºÚÉ«­¢+ØÀÌØí©µàôU  ½Õ¹ ÀÌØíѵÀ°Ä¤´Ä)½ÈÀÌØí¤ôÀѼU ½Õ¹ ÀÌØíÑ̤(]¡¥±MÑÉ%¹MÑÈ ÀÌØíÑÍlÀÌØí¥t°ÅÕ½ÐíìÅÕ½Ðì¤(½ÈÀÌØí¨ôÀѼ©µà(%MÑÉ¥¹%¹MÑÉ¥¹ ÀÌØíѵÁlÀÌØí¤°Át°ÀÌØíÑÍlÀÌØí©t¤Q¡¸(íQ½¼ÉÁ±ÑµÁ±ÑÝ¥Ñ Ù±Õ(¹%(íQ=

I cant test the code at the moment so typos and logical error might occur..:)

Edited by Uten
Link to comment
Share on other sites

Ups, sorry. Its supposed to be

$temp[2][2]oÝ÷ Ûú®¢×b-É÷öÜ(®H§«y«­¢+ØÀÌØíѵÁlÉulÉtômlÅÕ½Ðíí=5%9ôÅÕ½Ðì°ÅÕ½Ðíµä¹½µ¥¸¹½´ÅÕ½Ðít±lÅÕ½Ðíí9LÅôÅÕ½Ðì°ÅÕ½Ðí¹Ì¹å¸¹½´ÅÕ½Ðít±lÅÕ½Ðíí9LÉôÅÕ½Ðì°ÅÕ½Ðí¹Ì¹½µ¥¸¹½´ÅÕ½Ðít°lÅÕ½ÐííåôÅÕ½Ðì°ÅÕ½ÐìÄÀ¸À¸À¸ÌÅÕ½ÐíutoÝ÷ Ù«­¢+Ø)¥±
½Áä ÀÌØíÑÁ°°ÀÌØíµå¥ÈµÀìÅÕ½ÐìÀäÈí¹ÉÑÀäÈìÅÕ½ÐìµÀìÀÌØíáÑÝ¡½ÍµÀìÅÕ½Ðì¹ÅÕ½Ðì¤)}¥±IQ½ÉÉä ÀÌØíµå¥ÈµÀìÅÕ½ÐìÀäÈí¹ÉÑÀäÈìÅÕ½ÐìµÀìÀÌØíáÑÝ¡½ÍµÀìÅÕ½Ðì¹ÅÕ½Ðì°ÀÌØíÑ̤)½ÈÀÌØí¤ôÀѼU  ½Õ¹ ÀÌØíÑ̤(]¡¥±MÑÉ¥¹%¹MÑÈ ÀÌØíÑÍlÀÌØí¥t°ÅÕ½ÐíìÅÕ½Ðì¤(½ÈÀÌØí¨ôÀѼU   ½Õ¹ ÀÌØíѵÀİĤ´Ä($$%µÍ½à À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØí¤¤(%MÑÉ¥¹%¹MÑÈ ÀÌØíѵÀÅlÀÌØí¥t°ÀÌØíÑÍlÀÌØí©t¤Q¡¸(µÍ½à À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØíѵÀÉlÀÌØí¥t¤(¹%(íQ=

But it doesn't work at all.. The msgbox gives me "7" back, which isn't in the array...

Edited by Sypher
Link to comment
Share on other sites

Well $i show 7 because $datas[0 to 6] doesnt contaion a {

Try this

FileCopy($tpl, $mydir & "\generated\" & $extwhose & ".cfg")
_FileReadToArray($mydir & "\generated\" & $extwhose & ".cfg", $datas)
For $i = 0 to UBound($datas)
     MsgBox(0,0,"i=" & $i & " Datas[$i]=" & $datas[$i])
    While StringInStr($datas[$i], "{")
        For $j = 0 to UBound($temp1, 1) - 1
            msgbox(0,"",$i)
            If StringInStr($temp1[$i], $datas[$j]) Then
                 msgbox(0,"",$temp2[$i])
            EndIf
            ;TODO: Consider if you should skip the rest of the for loop if there is no more {'s
            ExitLoop
        Next
    Wend
Next

Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Link to comment
Share on other sites

Hmm it doesn't really do what it's supposed to

Func _parseTemplate()
    FileCopy($tpl, $mydir & "\generated\" & $extwhose & ".cfg")
    _FileReadToArray($mydir & "\generated\" & $extwhose & ".cfg", $datas)
    For $i = 0 To UBound($datas)
        While StringInStr($datas[$i], "{")
            MsgBox(0, "Found something on line " & $i, "Data:" & $datas[$i])
            For $j = 0 To UBound($temp1, 1) - 1
                If StringInStr($datas[$j], $temp1[$i]) Then
                    MsgBox(0, "Box2", "Temp1=" & $temp1[$i] & @LF & "Temp2=" & $temp2[$i])
                EndIf
                ;TODO: Consider if you should skip the rest of the for loop if there is no more {'s
            Next
            ExitLoop
        WEnd
    Next
EndFunc   ;==>_parseTemplate

This one gives me all the lines that contain a { ... So far so good.. But messagebox two never shows up. I tried reversing the StringInStr but that doesn't seem to do the trick..

It might be because i have:

{HOSTNAME} = "my.host.name"

And in the config a line that reads

hostname {HOSTNAME}

I only want to replace the {...} thing, not the whole line.

Edited by Sypher
Link to comment
Share on other sites

Well this works for me now :)

#include <file.au3>
#include <Array.au3>

Dim $datas[1]
Dim Const $temp[4][2] = [["{DOMAIN}","my.domain.com"],["{NS1}", "dns.dyn.com"],["{NS2}","dns.domain.com"], ["{FE9}","10.0.0.3"]]

FileCopy($tpl, $mydir & "\generated\" & $extwhose & ".cfg")
_FileReadToArray($mydir & "\generated\" & $extwhose & ".cfg", $datas)
For $i = 1 To UBound($datas) - 1
    For $j = 0 To UBound($temp, 1) - 1
        $datas[$i] = StringReplace($datas[$i],$temp[$j][0],$temp[$j][1],0)
    Next
Next
_ArrayDisplay($datas, "Test")

Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Link to comment
Share on other sites

Sorry again for misleading you with the array size. My original sample was with only two template keyword entries. And then I decided to add some more.

But you figured it out...:)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...