Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from CSV file to change multiple hostnames

Status
Not open for further replies.

Xrayhead

Programmer
Jul 19, 2002
24
GB
Hi All

I had a look yesterday at a few threads on the forum and pulled some examples of code but cant get them to work!

I want to create a template I can use for diferent task's but first want to get it to work with something simple.
I have a text file that reads as the following;

192.168.1.1,LDNP-swa01
192.168.1.2,LDNP-swa02
192.168.1.3,LDNP-swa03
192.168.1.4,LDNP-swa04
192.168.1.5,LDNP-swa05
192.168.1.6,LDNP-swa06
192.168.1.7,LDNP-swa07
192.168.1.8,LDNP-swa08
192.168.1.9,LDNP-swa09

I want to read from the text file and add the IP-address in at a point in the script and then add the hostname in at a second point in the script. Here's the code I have been trying to get to work;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* Script to change multiple hostname */

file = "c:\info.txt"

do while( lines(file) )

data=LINEIN(file)
Parse Var data v1,v2

CALL ZocTimeout 60
CALL ZocWaitForSeq 1

CALL ZocSend "ssh -l admin "
CALL ZocSend v1"^M"

CALL ZocWait "Password:"
CALL ZocSend "qwerty^M"

CALL ZocWaitForSeq 1
CALL ZocSend "conf t^M"

CALL ZocWait "4(config)#"
CALL ZocSend "hostname "
CALL ZocSend v2"^M"

CALL ZocWaitForSeq 1
CALL ZocSend "^M"

CALL ZocWaitForSeq 1
CALL ZocSend "end^M"

CALL ZocWaitForSeq 1
CALL ZocSend "wr^M"

CALL ZocWaitForSeq 2
CALL ZocSend "exit^M"

END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When I run the script it enters the whole line IE. "192.168.1.1,LDNP-swa01" I need to enter the IP Address at "v1" and the hostname at "v2".

I also need the script to loop untill it reaches the bottom of the text file, can this be done??

Xray
 
will give that a try today, many thanks!!

Xray
 
im working on a complicated parse script and have some questions

i create a buffer and parse incoming lines as a ; seperated value

this part of my script works fine i can get it to read line by line and pass the data exactly how i want..

now i need to either pass the line to another script for every line or if "part1" of line = "part1" of database then grab part3 of database as name...

i hope this makes sense

here is the start of the buffer process

x= ZocWait("[")
IF x\=640 THEN DO
data= ZocReceiveBuf(0)
howmany= ZocString("PARTCOUNT", data, CR)
SAY "-----"howmany-2 "Calls -----"
linecnt= 0
DO i= 2 TO howmany-1
line= ZocString("PART", data, i, CR)
/* put it into an array (lines) */
linecnt= linecnt+1
lines.linecnt= line /* this is REXX's way of saying lines[linecnt]= line */
END
lines.0= linecnt /* by convention the zero-element of an array stores the size */
DO i= 1 TO lines.0
line= lines.i /* get a copy of the i'th line */
/* Parse Each Line in As CDR Value Seperated by ; */
recordsequencenumber= ZocString("PART", ""||line"", 1, ";")
versionnumber= ZocString("PART", ""||line"", 2, ";")

so im parsing line for these 2 values ||recordsqeuencenumber and ||versionnumber


so if version number here matches idno in database i want it to associate name to that line or again if i can send the individual line to another script to parse i can do this on a single record base...

do while( lines(file) )
data=LINEIN(file)
PARSE VALUE data WITH idno1","type1","name1 1
IF idno1 = ZocString("PART", ""||line"", 14, ";") THEN LEAVE
END

i do this but it only reads through the database 1 time
it works for the 1st line and then stops completely which is why i assume i need to send it to another script to complete this part if i can send ||line out to another script

naturally i say what i find after this...
and i get the info i need for my 1st line

the last value in the CSV is line idno,type,N/A for name, and i get N/A for all lines other than line 1

i need a different way to parse the database it would seem
 
yes i am doing the same parsing a CSV, although im over complicating it im sure im trying to parse the csv for each line in my script grabs and having the problem where it will only parse for the 1st line and then it stops
 
also

do while( lines(file) )
dat1=LINEIN(file)
PARSE VALUE dat1 WITH idno1","type1","name1 1
IF idno1 = ZocString("PART", ""||line"", 14, ";") THEN LEAVE
END

the variable was written wrong on this
 
It is a good thing you said that papadba
i have run a couple of traces but carefully looking through one i found what is happening


so for the 1st line it parses the entire csv till it finds the line it wants

the problem is for the second line its not starting over instead it is picking up where it left off on line.1

so how would i get it to restart this process for each line is where im at now
 
Ok i found a better way to do it

DO FOREVER
ln= LINEIN(file)
IF STREAM(file, "S")\="READY" THEN LEAVE
/* process line of file (ln) here */
PARSE VALUE ln WITH idno1","type1","name1
IF idno1 = ""||trunkidorigination"" THEN LEAVE
END

this reads the entire file each time as opposed to
do while(lines(file)

just tedious to put these things together sometimes, hope this helps anyone with similar problems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top