Sign in to follow this  
Followers 0
ne0trace

Stringreplace - IP address into an xml file

7 posts in this topic

#1 ·  Posted (edited)

Hello,

we are currently using a program which has a serious bug. Sometimes it won't recognize the name of the computer as the name of the server (a client program). If that happens you have to manually change the name into the ip adress. This is only a temporary solution as we use a DHCP server.

So my thought was to replace a certain string in the text "<ServerName>slved_p08x0010</ServerName>" (without the quotes) with the actual ip adress. But the ServerName variable can be different with every computer (can be an IP or a name). As some computers may have their cable plugged into a different adapter, it would be nice to have some check that only ip adresse like 192.168.x.x apply.

I did some tests (with @IPAddress1) but nothing that could be called a productive code. So if anyone could give me a hint on achieving this, I would be very happy. :)

Btw. I pasted the content of the XML file below.

Thank you

Felix

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE SystemConfiguration [
    <!ELEMENT SystemConfiguration (GeneralCfg*, TimerCfg*, LogCfg*, AppDetails*)>
    <!ELEMENT GeneralCfg (ManagerPort, MaxBlockSize, NetworkMask)>
    <!ELEMENT ManagerPort (#PCDATA)>
    <!ELEMENT MaxBlockSize (#PCDATA)>
    <!ELEMENT NetworkMask (#PCDATA)>
    <!ELEMENT TimerCfg (AckTimeout, AckRetries, FastTimeout)>
    <!ELEMENT AckTimeout (#PCDATA)>
    <!ELEMENT AckRetries (#PCDATA)>
    <!ELEMENT FastTimeout (#PCDATA)>
    <!ELEMENT LogCfg (LogError, LogWarning, LogInfo, LogDebug, LogDebugex)>
    <!ELEMENT LogError (#PCDATA)>
    <!ELEMENT LogWarning (#PCDATA)>
    <!ELEMENT LogInfo (#PCDATA)>
    <!ELEMENT LogDebug (#PCDATA)>
    <!ELEMENT LogDebugex (#PCDATA)>
    <!ELEMENT AppDetails ANY>
    ]>
<SystemConfiguration>
    <GeneralCfg>
        <ManagerPort>3234</ManagerPort>
        <ServerPort>3233</ServerPort>
        <MaxBlockSize>128000</MaxBlockSize>
        <NetworkMask>255.255.255.0</NetworkMask>
        <HttpRedirection>http://www.discreet.com</HttpRedirection>
    </GeneralCfg>
    <TimerCfg>
        <AckTimeout>20000</AckTimeout>
        <AckRetries>6</AckRetries>
        <FastTimeout>4000</FastTimeout>
    </TimerCfg>
    <LogCfg>
        <LogError>1</LogError>
        <LogWarning>1</LogWarning>
        <LogInfo>1</LogInfo>
        <LogDebug>0</LogDebug>
        <LogDebugex>0</LogDebugex>
        <LogMaxFileSize>10485760</LogMaxFileSize>
    </LogCfg>
    <AppDetails>
        <ServerSettings>
            <ManagerName>bb_manager</ManagerName>
            <AutoSearch>0</AutoSearch>
[b]         <ServerName>slved_p08x0010</ServerName>[/b]
            <ServerMAC>001EC9FF02B7</ServerMAC>
            <Description></Description>
        </ServerSettings>
        <SrvGUISettings>
            <LogErrorScr>1</LogErrorScr>
            <LogWarningScr>1</LogWarningScr>
            <LogInfoScr>1</LogInfoScr>
            <LogDebugScr>0</LogDebugScr>
            <LogDebugexScr>0</LogDebugexScr>
            <LogScrSize>2048</LogScrSize>
            <WindowX>189</WindowX>
            <WindowY>16</WindowY>
            <WindowW>800</WindowW>
            <WindowH>600</WindowH>
            <FontSize>2</FontSize>
            <AutoScroll>1</AutoScroll>
            <FirstTime>0</FirstTime>
            <TimeStamp>No</TimeStamp>
            <DateStamp>No</DateStamp>
            <ConfirmExit>1</ConfirmExit>
        </SrvGUISettings>
    </AppDetails>
</SystemConfiguration>
Edited by ne0trace

Share this post


Link to post
Share on other sites



This might be helpful to see if the computer has an IP within 192.168. range:

If StringInStr(@IPAddress1, "192.168.") Then
    MsgBox(0, "Computer IP", @IPAddress1)
ElseIf StringInStr(@IPAddress2, "192.168.") Then
    MsgBox(0, "Computer IP", @IPAddress2)
ElseIf StringInStr(@IPAddress3, "192.168.") Then
    MsgBox(0, "Computer IP", @IPAddress3)
ElseIf StringInStr(@IPAddress4, "192.168.") Then
    MsgBox(0, "Computer IP", @IPAddress4)
Else
    MsgBox (16, "IP Error", "None of your computer's IP is in 192.168.x.x range")
EndIf

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Great! So far this seems to work:

If StringInStr(@IPAddress1, "192.168.") Then
    $Ip = @IPAddress1
ElseIf StringInStr(@IPAddress2, "192.168.") Then
    $Ip = @IPAddress2
ElseIf StringInStr(@IPAddress3, "192.168.") Then
    $Ip = @IPAddress3
ElseIf StringInStr(@IPAddress4, "192.168.") Then
    $Ip = @IPAddress4
Else
    MsgBox(16, "IP Error", "None of your computer's IP is in 192.168.x.x range")
EndIf

MsgBox(0, "Computer IP", $Ip)

Thank you

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

So I am nearly done but the StringReplace doesn't work with Wildcards... Any idea?

#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.2.12.1
    Author:         myName
    
    Script Function:
    Template AutoIt script.
    
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

$XMLfile = "c:\test.xml"

If Not FileExists("c:\test.xml") Then
    MsgBox(16, "Backburner XML Fix", "No Backburner file exists", 5)
Else
    FileCopy($XMLfile, "c:\test.bak")
    
EndIf


If StringInStr(@IPAddress1, "192.168.") Then
    $Ip = @IPAddress1
ElseIf StringInStr(@IPAddress2, "192.168.") Then
    $Ip = @IPAddress2
ElseIf StringInStr(@IPAddress3, "192.168.") Then
    $Ip = @IPAddress3
ElseIf StringInStr(@IPAddress4, "192.168.") Then
    $Ip = @IPAddress4
Else
    MsgBox(16, "IP Error", "None of your computer's IP is in 192.168.x.x range")
EndIf

$Rawtext = FileRead($XMLfile)
$Outputtext = StringReplace($Rawtext, "<ServerName>*</ServerName>", "<ServerName>" & $Ip & "</ServerName>")
FileDelete($XMLfile)
FileWrite($XMLfile, $Outputtext)
Edited by ne0trace

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

2 ways to get this done:

- use of an xml UDF (search for xml in Example Scripts Forum)

- string split using "ServerName>" as delimiter

Using the 2nd method the return will be "yourtextHere</"

#Include <Array.au3>

$String = "uy7r8346 75 34 <ServerName> My Computer </ServerName>kfdhgjk78 dfgn 8dg dfio"
$IP = "192.168.0.100"
$temp = StringSplit($String, "ServerName>", 1)
_ArrayDisplay($temp)
$computerName = StringTrimRight($temp[2], 2)
MsgBox(0, "Computer Name", $computerName)
$output = StringReplace($String, $computerName, $IP)
MsgBox(0, "New String", $output)

I've left the debugging lines inside to show you what happens. You only need to "adapt" this code to suit your script :)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Hey enaiman,

I didn't have time to implement your code but it looks great. I was really excited to check out your approach with splitting teh strings. I hope to get it working this evening.

Thanks so far

Felix

Share this post


Link to post
Share on other sites

Welcome buddy :)

I know you will get it working.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0