#127234 - 2004-09-30 01:55 PM
read a list
|
Anonymous
Anonymous
Unregistered
|
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
|
Top
|
|
|
|
#127236 - 2004-09-30 03:29 PM
Re: read a list
|
Anonymous
Anonymous
Unregistered
|
thanks i used your example in my script, but it doesn't work.
sorry it's partly in german
|
Top
|
|
|
|
#127237 - 2004-09-30 03:30 PM
Re: read a list
|
Anonymous
Anonymous
Unregistered
|
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
|
Top
|
|
|
|
#127239 - 2004-09-30 03:46 PM
Re: read a list
|
Anonymous
Anonymous
Unregistered
|
i think so...
$Liste = "U:\kix\liste.txt"
|
Top
|
|
|
|
#127241 - 2004-10-01 08:14 AM
Re: read a list
|
Anonymous
Anonymous
Unregistered
|
ok, thanks a lot
|
Top
|
|
|
|
#127242 - 2004-10-01 01:18 PM
Re: read a list
|
Anonymous
Anonymous
Unregistered
|
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
|
Top
|
|
|
|
#127244 - 2004-10-01 01:40 PM
Re: read a list
|
Anonymous
Anonymous
Unregistered
|
you recalled correctly, because now it works.
how can i convert in udf?
|
Top
|
|
|
|
#127245 - 2004-10-01 01:56 PM
Re: read a list
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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)
|
Top
|
|
|
|
#127246 - 2004-10-01 02:00 PM
Re: read a list
|
nic852
Fresh Scripter
Registered: 2004-10-01
Posts: 6
Loc: Deutschland, Burghausen
|
ok, i will try it
|
Top
|
|
|
|
#127247 - 2004-10-01 02:58 PM
Re: read a list
|
nic852
Fresh Scripter
Registered: 2004-10-01
Posts: 6
Loc: Deutschland, Burghausen
|
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!"
Edited by nic852 (2004-10-01 03:05 PM)
|
Top
|
|
|
|
#127250 - 2004-10-01 03:43 PM
Re: read a list
|
nic852
Fresh Scripter
Registered: 2004-10-01
Posts: 6
Loc: Deutschland, Burghausen
|
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
|
Top
|
|
|
|
#127253 - 2004-10-01 04:46 PM
Re: read a list
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 283 anonymous users online.
|
|
|