Page 1 of 2 12>
Topic Options
#127234 - 2004-09-30 01:55 PM read a list
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
#127235 - 2004-09-30 02:26 PM Re: read a list
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
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

_________________________
Eric

Top
#127236 - 2004-09-30 03:29 PM Re: read a list
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
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
#127238 - 2004-09-30 03:34 PM Re: read a list
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Are you sure you're reading from the right file?

Code:

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

_________________________
Eric

Top
#127239 - 2004-09-30 03:46 PM Re: read a list
Anonymous
Unregistered


i think so...

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

Top
#127240 - 2004-09-30 03:59 PM Re: read a list
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
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.
_________________________
Eric

Top
#127241 - 2004-10-01 08:14 AM Re: read a list
Anonymous
Unregistered


ok, thanks a lot
Top
#127242 - 2004-10-01 01:18 PM Re: read a list
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
#127243 - 2004-10-01 01:30 PM Re: read a list
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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.

Top
#127244 - 2004-10-01 01:40 PM Re: read a list
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 Offline
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 Offline
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 Offline
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
#127248 - 2004-10-01 03:31 PM Re: read a list
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
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")
_________________________
Eric

Top
#127249 - 2004-10-01 03:35 PM Re: read a list
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
GOTOs are an even greater sin than GOSUBs and should not be used.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#127250 - 2004-10-01 03:43 PM Re: read a list
nic852 Offline
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
#127251 - 2004-10-01 03:59 PM Re: read a list
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
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

_________________________
Eric

Top
#127252 - 2004-10-01 03:59 PM Re: read a list
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
You should NEVER jump into a loop with GOTO! The sooner you learn how not to spaghetti code with GOTO, the better.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#127253 - 2004-10-01 04:46 PM Re: read a list
Richard H. Administrator Offline
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
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 312 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.047 seconds in which 0.019 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org