**DONOTDELETE**
(Lurker)
2004-09-30 01:55 PM
read a list

hi

i have a script which changes the ip-adress in our network.
and i have a txt.-file where are in each line is the computername, the new ip-adress, the new DefaultGateway.
( computername; 168.18.68.11; 168.18.68.254)
and the script has to make 3 variables out of one line, and then the next line and so on

can someone help me


maciep
(Korg Regular)
2004-09-30 02:26 PM
Re: read a list

Something like this?

Code:

$file = "c:\list.txt"
$ = open(1, $file)
$aCurLine = split(readline(1),';')
while not @error
$computer = trim($aCurLine[0])
$newIP = trim($aCurLine[1])
$newGW = trim($aCurLine[2])

? "Computer: " $computer
? "New IP: " $newIP
? "New GW: " $newGW

$aCurLine = split(readline(1),';')
loop



**DONOTDELETE**
(Lurker)
2004-09-30 03:29 PM
Re: read a list

thanks
i used your example in my script, but it doesn't work.

sorry it's partly in german


**DONOTDELETE**
(Lurker)
2004-09-30 03:30 PM
Re: read a list

oops i forget to post my script

;-----------------------------------------------------------------------------
;Programm initialisiern
;-----------------------------------------------------------------------------
Color y+/b
;Übergabe-Parameter (zu Testzwecken, ansonsten auskommentieren)
$SoftwareSource = "U:\kix\"

;Konstanten definieren
$ScrName = "ip1.script"
$INIFile = $SoftwareSource+"\NicAD2.ini"
$LOGFile = "U:\kix\remoteip.log"
$Liste = "U:\kix\liste.txt"

;Variablen initialisieren
$ScriptError = 0
$regerr = 0

;Log-File öffnen
$ret = Open(1, $LOGFile, 5) ;wenn Log-File nicht vorhanden, dann neues Log-File erzeugen

;-----------------------------------------------------------------------------
;PC-Namen und neue IP-Adresse auslesen
;-----------------------------------------------------------------------------
$ = open(2, $Liste) ;opens the file liste.txt
$Line = split(readline(1),"; ")
while not @error
$PC = trim($line[0])
$ret = writeline (1, "PC-Name ausgelesen...$PC")
$newIP = trim($line[1])
$ret = writeline (1, "neue IP ausgelesen...$newIP")
$newGW = trim($line[2])
? "Computer: " $pc
? "New IP: " $newIP
? "New GW: " $newGW
$Line = split(readline(1),"; ")
Gosub "Sub_Network"
Loop
;Ende
;-----------------------------------------------------------------------------
;Programm beenden
;-----------------------------------------------------------------------------
:ENDE

;Logfile schließen
$ret = WriteLine (1, "---"+$ScrName+" done----------------------------------"+@CRLF+@CRLF)
$ret = Close (1)

;Logfile anzeigen (nur zu Testzwecken)
Shell "cmd.exe /C start %SystemRoot%\notepad.exe "+$LOGFile
Sleep 1 ;sonst wird das Logfile zu schnell gelöscht

Exit $ScriptError


;-----------------------------------------------------------------------------
;Netzwerkkarten auslesen und ggf. konfigurieren
;-----------------------------------------------------------------------------
:Sub_Network
;Variablen initialisieren
$Index = 0
$RootKeyNICs = "\\$PC\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\"
$subkey = EnumKey($RootKeyNICs, $Index)

;nach Netzwerkkarten suchen
While @ERROR = 0

;Variablen initialisieren
$EthID = ""
$NIC_Config = ""
$NIC_NoConfig = ""

;Netzwerkkarten-ID (ServiceName) auslesen
$EthID = ReadValue($RootKeyNICs+$subkey,"ServiceName")

;wenn Netzwerkkarte vorhanden,
If $EthID <> ""

;Netzwerkkarten-Description auslesen
$Description = ReadValue($RootKeyNICs+$subkey,"Description")
$ret = WriteLine (1, "Netcard: $Description"+@CRLF)
$ret = WriteLine (1, "NetcardID: $EthID"+@CRLF+@CRLF)


;überprüfen, ob Netzwerkkarte bekannt und für Konfiguration definiert
$NIC_Config = ReadProfileString($INIFile, "Umzustellende Netzwerkkarten" ,$Description)
If $NIC_Config = "1"
$ret = WriteLine (1, " Netzwerkkarte erkannt und fuer Konfiguration definiert"+@CRLF)
Else
;überprüfen, ob Netzwerkkarte bekannt, aber nicht für Konfiguration definiert
$NIC_NoConfig = ReadProfileString($INIFile, "Nicht-umzustellende Netzwerkkarten" ,$Description)
If $NIC_NoConfig = "1"
$ret = WriteLine (1, " Netzwerkkarte bekannt, aber nicht fuer Konfiguration definiert"+@CRLF+@CRLF)
Else
;Netzwerkkarte unbekannt, => in INI-File als neue Netzwerkkarte eintragen
$ret = WriteLine (1, "Netzwerkkarte nicht bekannt, bitte in Datei "+$INIFile+" eintragen."+@CRLF+@CRLF)
;$ret = WriteProfileString($INIFile," Neue Netzwerkkarten",$Description,$EthID)
$ret = WriteLine(1, "[Neue Netzwerkkarte]"+@CRLF+$Description+"=1"+@CRLF)
$ScriptError=2
GoTo ENDE
EndIf
EndIf

EndIf

;wenn Netzwerkkarte für Konfiguration definiert
If $NIC_Config <> ""

;entsprechende Aktionen ausführen
Select
Case $action="ip" ;IP-Adresse ändern
$ret = WriteLine (1, " IP-Adresse wird nun geändert..."+@CRLF+@CRLF)
Gosub "SUB_ConfigureIP"

Case $action="info"
$ret = WriteLine (1, " keine Aktion durchgeführt (info)..."+@CRLF)
EndSelect
EndIf

If $regerr>0
$ret = WriteLine(1, "RegError: es konnten "+$regerr+" Registry-Einträge nicht vorgenommen werden"+@CRLF)
$ScriptError=6
GoTo ENDE
EndIf

;nächsten Subkey auslesen
$ret = WriteLine (1, @CRLF)
$Index = $Index+1
$subkey = EnumKey($RootKeyNICs, $Index)

Loop
Return
;Ende
;-----------------------------------------------------------------------------
;Subroutine für IP-Adressenänderung
;-----------------------------------------------------------------------------
:SUB_ConfigureIP


$NIC_Parameter="\\$pc\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"+$EthID+"\Parameters\Tcpip"
$regerr = $regerr + (WriteValue($NIC_Parameter, "IPAddress", "$newIP", "REG_MULTI_SZ")<>0)
$regerr = $regerr + (WriteValue($NIC_Parameter, "DefaultGateway", "$newGW", "REG_MULTI_SZ")<>0)
$ret = writeline (1, " neue IP-Adresse:...$newIP"+@CRLF+@CRLF)
$ret = writeline (1, " neuer Gateway:...$newGW"+@CRLF+@CRLF)

$TCPIP_Parameter="\\$pc\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"+$EthID
$regerr = $regerr + (WriteValue($TCPIP_Parameter, "IPAddress", "$newIP", "REG_MULTI_SZ")<>0)
$regerr = $regerr + (WriteValue($TCPIP_Parameter, "DefaultGateway", "$newGW", "REG_MULTI_SZ")<>0)
$ret = WriteLine (1, " IP-Change von "$PC" abgeschlossen"+@CRLF)

Return
;Ende


maciep
(Korg Regular)
2004-09-30 03:34 PM
Re: read a list

Are you sure you're reading from the right file?

Code:

$ = open(2, $Liste) ;opens the file liste.txt
$Line = split(readline(1),"; ")



**DONOTDELETE**
(Lurker)
2004-09-30 03:46 PM
Re: read a list

i think so...

$Liste = "U:\kix\liste.txt"


maciep
(Korg Regular)
2004-09-30 03:59 PM
Re: read a list

Let me rephrase, you are not reading from the correct file. You need to change your $Line assignments from

Code:

$Line = split(readline(1),"; ")


to
Code:

$Line = split(readline(2),"; ")



Right now, you're trying to read from the file you are writing to.


**DONOTDELETE**
(Lurker)
2004-10-01 08:14 AM
Re: read a list

ok, thanks a lot

**DONOTDELETE**
(Lurker)
2004-10-01 01:18 PM
Re: read a list

hi,

reading the list works now, the script takes 3 variabels out of one line, but it makes only the first line. the loop doesn't work

code:

$liste = "U:\kix\liste.txt"
$ = open(2, $liste) ;opens the file liste.txt
$Line = split(readline(2),"; ")
while @ERROR = 0
$PC = trim($line[0])
$ret = writeline (1, "PC-Name ausgelesen.........$PC"+@CRLF)
$newIP = trim($line[1])
$ret = writeline (1, "neue IP ausgelesen.........$newIP"+@CRLF)
$newGW = trim($line[2])
$ret = writeline (1, "neuer Gateway ausgelesen...$newGW"+@CRLF+@CRLF)
? "Computer: " $pc
? "New IP: " $newIP
? "New GW: " $newGW
? ""
$Line = split(readline(2),"; ")
Gosub "Sub_Network"
:PCIP
Loop
;Ende


Richard H.Administrator
(KiX Supporter)
2004-10-01 01:30 PM
Re: read a list

If I recall correctly, there was a bug some time back that caused loop structures to fail when a subroutine was called .

To test this comment out the 'Gosub "Sub_Network"' and see if the loop displays all the values you expect.

If it does, just convert the subroutine to a UDF.


**DONOTDELETE**
(Lurker)
2004-10-01 01:40 PM
Re: read a list

you recalled correctly, because now it works.

how can i convert in udf?


Richard H.Administrator
(KiX Supporter)
2004-10-01 01:56 PM
Re: read a list

The UDF will look something like this:
Code:
Function ConfigureIP($sPC,$sEthID,$sNewIp,$sNewGW)
Dim $sRegKey,$iDiscard

$ConfigureIP=0

$sRegKey="\\"+$sPC+"\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"+$sEthID+"\Parameters\Tcpip"
$ConfigureIP = $ConfigureIP + (WriteValue($sRegKey, "IPAddress", $sNewIP, "REG_MULTI_SZ")<>0)
$ConfigureIP = $ConfigureIP + (WriteValue($sRegKey, "DefaultGateway", $sNewGW, "REG_MULTI_SZ")<>0)
$iDiscard = WriteLine(1, " neue IP-Adresse:..."+$sNewIP+@CRLF+@CRLF)
$iDiscard = WriteLine(1, " neuer Gateway:..."+$sNewGW+@CRLF+@CRLF)

$sRegKey="\\"+$sPC+"\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"+$sEthID
$ConfigureIP = $ConfigureIP + (WriteValue($sRegKey, "IPAddress", $sNewIP, "REG_MULTI_SZ")<>0)
$ConfigureIP = $ConfigureIP + (WriteValue($sRegKey, "DefaultGateway", $sNewGW, "REG_MULTI_SZ")<>0)
$iDiscard = WriteLine (1, " IP-Change von "+$sPC+" abgeschlossen"+@CRLF)

Exit $ConfigureIP
Return



Now, instead of the gosub, call it like this:
Code:
$regerr=ConfigureIP($ps,$EthID,$newIP,$newGW)



nic852
(Fresh Scripter)
2004-10-01 02:00 PM
Re: read a list

ok, i will try it

nic852
(Fresh Scripter)
2004-10-01 02:58 PM
Re: read a list

I wasn't able to convert my subroutine into a UDF so I tried something else.
i changed the while-loop command in a do-until command but it still doesn't work.
so instead of gosub I use now goto
Code:
 

$liste = "U:\kix\liste.txt"
$ = open(2, $liste) ;opens the file liste.txt
$Line = split(readline(2),"; ")
:PCIP
do
$PC = trim($line[0])
$ret = writeline (1, "PC-Name ausgelesen.........$PC"+@CRLF)
$newIP = trim($line[1])
$ret = writeline (1, "neue IP ausgelesen.........$newIP"+@CRLF)
$newGW = trim($line[2])
$ret = writeline (1, "neuer Gateway ausgelesen...$newGW"+@CRLF+@CRLF)
? "Computer: " $pc
? "New IP: " $newIP
? "New GW: " $newGW
? ""
$Line = split(readline(2),"; ")
GoTo network
until @error <> 0
;Ende


and under goto network
Code:
 

:Network
gosub "Sub_Network"
goto PCIP


now the loop-function works, but when the script arrives the end of the textfile i get a "ERROR : array reference out of Bounds!"


maciep
(Korg Regular)
2004-10-01 03:31 PM
Re: read a list

I think the problem is that your subroutines may be setting @error which when you is then used in your loop instead of the @error from the $Line assignment.

Can you try putting the $Line assignment right before your "Loop" statement (right after the ":PCIP")


Les
(KiX Master)
2004-10-01 03:35 PM
Re: read a list

GOTOs are an even greater sin than GOSUBs and should not be used.

nic852
(Fresh Scripter)
2004-10-01 03:43 PM
Re: read a list

i tried it, but it doesn't.
it's still the same problem .

i put the PCIP under the "do"

Code:

$ = open(2, $liste) ;opens the file liste.txt
$Line = split(readline(2),"; ")
do
:PCIP
$PC = trim($line[0])
$ret = writeline (1, "PC-Name ausgelesen.........$PC"+@CRLF)


now the whole scripts works, except the end.
it changes all IP-Adresses, and takes the whole list.
but instead of going to end when the list worked up, i get this error


maciep
(Korg Regular)
2004-10-01 03:59 PM
Re: read a list

This is what i meant. And Les is right, you really should try to avoid goto's where ever possible (which is basically anywhere)

Code:

$liste = "U:\kix\liste.txt"
$ = open(2, $liste) ;opens the file liste.txt
$Line = split(readline(2),"; ")
while @ERROR = 0
$PC = trim($line[0])
$ret = writeline (1, "PC-Name ausgelesen.........$PC"+@CRLF)
$newIP = trim($line[1])
$ret = writeline (1, "neue IP ausgelesen.........$newIP"+@CRLF)
$newGW = trim($line[2])
$ret = writeline (1, "neuer Gateway ausgelesen...$newGW"+@CRLF+@CRLF)
? "Computer: " $pc
? "New IP: " $newIP
? "New GW: " $newGW
? ""
Gosub "Sub_Network"
:PCIP
$Line = split(readline(2),"; ")
Loop



Les
(KiX Master)
2004-10-01 03:59 PM
Re: read a list

You should NEVER jump into a loop with GOTO! The sooner you learn how not to spaghetti code with GOTO, the better.

Richard H.Administrator
(KiX Supporter)
2004-10-01 04:46 PM
Re: read a list

Code:
I wasn't able to convert my subroutine into a UDF



Why

You should have been able to drop the code I provided into your script without modification. (Well, maybe a little bug fixing, I couldn't test it here)

Add the "Function ... EndFunction" code to the end of your script.

Replace your Gosub with the function call and you're done.


Sealeopard
(KiX Master)
2004-10-02 02:13 AM
Re: read a list

See also the CheckNICs() UDF.