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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping a series of Telnet entries to connect and run same command 1

Status
Not open for further replies.

malium

ISP
Jun 26, 2003
23
0
0
US
I have a working script that telnets to multiple sites and runs commands while dumping the results to a file.

I have about 12 sites it connects to and I know a cleaner way to do this and be scalable for new sites is to just have it loop the telnet entries in my Connection Directory. Currently I'm manually pasting the the same dial command sequence multiple times and changing the SiteName.

It works now, but I just would like some tips on how I can condense it to just dial every entry in the Telnet Connection Directory, or to allow me to set a bunch of SiteName1, SiteName2 strings to be inserted in a loop until all are dialed and run.



proc main

set terminal scroll on
set capture recordmode SCREEN
set capture path "c:\router_data"
set capture file "resetflap_all.txt"
set capture overwrite ON ; off will append
capture on
yield
set aspect spawn ON ;allows script to spawn scripts
set dialentry scriptstart CONNECTED

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; dials entry from Connection Directory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dial TELNET "SiteName1"
while $DIALING
yield
endwhile
waitfor "#"
transmit "show calendar^M"
waitfor "#"

transmit "clear cable flap all^M"
waitfor "#"
transmit "exit^M"


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

dial TELNET "SiteName2"
while $DIALING
yield
endwhile
waitfor "#"

transmit "clear cable flap all^M"
waitfor "#"
transmit "exit^M"


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

capture off ; Close the capture file.
endproc


 
You can use the dialcount and dialname commands to iterate through all of the telnet entries in your connection directory. Here is some code stolen from a sample on my site that shows the syntax:



dialcount TELNET iEntries ;Get number of entries in specified dialclass
for iCount = 0 upto (iEntries - 1)
dialname TELNET iCount sName ;Get entry name corresponding to index number
endfor

where iEntries and iCount are integer variables and sName is a string variable.

You could place the common commands that are currently being executed in your script after the dialname command above.

aspect@aspectscripting.com
 
Thanks!!!

It took me a few tests to figure out where to put what but I got it working. Here's what I came up with - feel free to add back to your example resources if you'd like to.

This dials each Telnet entry and runs a command sequence, exits and then loops through connecting to the rest of the entries to run the same command. It logs this to a file and opens the file with notepad when done.


proc main
integer iEntries, iCount
string sName

set capture recordmode SCREEN
set capture path "c:\data"
set capture file "info.txt" ;
set capture overwrite ON ; off will append
capture on ; Open up the capture file.
yield ; Yield processing time
set aspect spawn ON ;allows script to spawn scripts
set dialentry scriptstart CONNECTED ; runs default script for entry

;Loops through Connection Directory Telnet entries
dialcount TELNET iEntries ;Get number of entries in specified dialclass
for iCount = 0 upto (iEntries - 1)
dialname TELNET iCount sName ;Get entry name corresponding to index number

dial TELNET sName
while $DIALING
yield
endwhile
waitfor "#"
transmit "show calendar^M"
waitfor "#"
transmit "exit^M"
endfor

capture off ; Close the capture file.
run "notepad.exe c:\data\info.txt"

endproc


Questions and comments:

Capture on /off never seems to work for me within a script - for example, I'd like to use:

waitfor "#"
capture on
transmit "show calendar^M"
waitfor "#"
capture off
transmit "exit^M"

But it doesn't seem effective. When I pad the capture statements with a pause of 1-3 seconds after it I can see the capture file open/close on status at the bottom of Procomm. But my capture file still has all the extra lines before and after my capture on/off.

How can I toggle capture on/off effectively in that context?

 
The way the capture file works is that any data on the screen when you open or close the capture file, even if the data was received before the capture on statement was issued, will appear in the file. You can have your script issue the "clear" ASPECT command before the capture on statement to clear the screen. This will keep that data from appearing in the capture file.


aspect@aspectscripting.com
 
Thanks, that was clearly something fundamental I was missing that I needed to know.

That got the capture on/off working now but saw some odd stuff along the way. First I noticed that I was getting blank results and traced it back to finding that when capture on/off within the dial loop it opens and closes the default capture file and ignores the one I had set above. It was cpaturing but to the wrong file.

I can work around this by moving the set capture file name down after the dial command. But it seem odd to me as it appears Procomm keeps the set capture path because my deault capture files in the Directory don't target the same folder. Not sure if this is a bug or a feature. :^) It's one of those things that doesn't matter much once you understand what it's doing but from behavior it appears that using the dial entries loop the way I am overrides the set capture file name with the default capture file.

The other new problem now is that the resulting capture file with the on/off to selective clearing also results in a capture file with huge gaps of blank lines. This makes for as diffiult a readability of the results as leaving in all the login/out lines I was trying to strip.

Is there an easy way to process the capture file after I've closed it and run the dial loop where I can strip out any blank lines in the file, or delete all but one blank line any time there is more than one blank line, or replace them with a separator line such as
------------------------------------------------------
to break up my results better?
 
Regarding the wrong capture file being used, the most likely cause for this is that your Connection Directory entries likely have a capture file of some sort specified, so that when you make a connection, the script is using that capture file until you use the set command.

Unfortunately, there's not really a way to get rid of the blank lines before the capture file itself is saved. However, you could open the file afterwards using the fopen command, go through line by line with the fgets command, and get the length of the line read. If you specify the TEXT argument to the fopen command, then a blank line will have a length of zero since the carriage return and linefeed will be stripped off. You could then use this fact to create a second text file into which you write all non-zero lines. There is also a method to delete text from a file - I think I have an example or two of this on my site.

Finally, if you want to just use the separator line as you mentioned, you could use the capturestr command to add that line at the appropriate place in your script.


aspect@aspectscripting.com
 
Thanks again for the tips.

Regarding the wrong capture file being used, the most likely cause for this is that your Connection Directory entries likely have a capture file of some sort specified, so that when you make a connection, the script is using that capture file until you use the set command.

Exactly, but I think it's a bug because:

1) there is a capture file specified in the Directory entry, but when I use the original script above with the file set and capture on before the dial loop connects to the various sites captures go to the file specified in the script.

2) when I move the Capture on down to inside the dial loop it still keeps the path set in the script I'm running, but it loses the capture file name I set and uses the default from the Directory unless I explicitly set the file name again in the dial loop.

I think it's something about the for statement that's causing it to drop down to the default capture file and instead of my specific file and I end up with a dozen different capture files - in the path I set, matching the unique capture names in the Directory config.

To me that's a bug because the Directory Entry capture file only overrides the one I set depending on where my capture is turned on. But that's ok since it's easy to work around.
 
I haven't dug into this more, but what I think is happening is that if the capture file specified in the script is turned on, then the Connection Directory is not allowed to override the existing value (because one is already open). However, if the capture file is not opened, then the Connection Directory's value is used.


aspect@aspectscripting.com
 
That makes sense. It's trivial to work around, just confusing when you first see it happening.

Thanks for your great tips and examples web pages, I keep finding useful stuff when I go back and read again when I'm looking to implement new ideas. I still can't get over how powerfull a tool Procomm can be using Apsect and how easy it is for a non-programmer like myself to work with and adapt it to my needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top