![]() |
|
#1
|
|||
|
|||
|
Logging output to logfile and clipboard simultaneously
Hi All
I am not quite sure how to tackle this so I will start by posting my script warts and all and see what comes of this. I suspect this script is next to impossible to read sorry for that. The good news is it does work for me I would be glad if anyone who wants to make use of it and can do so successfully. What it does Consider the following: You have a telnet session to an IOS device. You type in a "show run". At this point to the left of the cursor is a set of characters of the form: Hostname#show run The cursor is to the RHS of the "show run" that you have just entered. At this point you run the script. I have a keyboard shortcut Ctrl-Enter setup to run this script. The script collects the string to the left of the cursor. It uses the "#" to sort out the hostname and the command that you have just entered. The script then does something like this: - Stops the existing logging setup; - Appends the "show command" string to the end of the existing logfile name; - Starts logging again (with new logfile name); - Sends a CR. This sends the "show run" or other command to the remote host; - Waits for all the output to arrive; - Closes the logfile; - Starts the orginal logfile and logging going; - Script ends The outcome is when I do a "show run" or other and in my case hit Ctrl-Enter, I get a log file with the command output. I have improved it over time to account for various things. For example if the command output is long (i.e. more that one screen) a Cisco switch/router will pause with the keystring "---More---" The script now accommodates this. Also it accommodates other kinds of prompt characters aside from "#". e.g. A Cisco Wireless LAN Controller prompt is one of these ">". Etc. It is not perfect but it does work pretty well. What I want to do now and need help with Now what I want to do is add the output to the windows clipboard as well. e.g. If I do a "show version" it logs the output nicely to a discrete logfile. Great. But I find more often than not I go to the file, open it, copy it's contents, close the file, and paste the contents soemwhere e.g. a word file. So I want the command output copied to clipboard automatically. Possible ways to do this (1) ReadString" An examination of my script will show use of the "WaitforStrings" method. This is how I deal with the need to hit the space bar to make long outputs arrive screen by screen. The ReadString method I have had a look at. It looks useful but it would mean a whole new approach to my script. More or less a rewrite. (2) Read off the screen I am not sure how I would tackle this either. I know there are ways to do this. I was interesting to note the following which would seem to present a problem given my existing approach. I got this out of the Scripting Essentials PDF: Quote:
So my script is halted in fact while the output is arriving from the remote host to the secureCRT session. The logging facility is saving the output. 3 Read back the contents of the logfile at the end This would seem the only way to go that would not mean rewriting my entire script. I have not looked into it in detail yet. A few comments. I may have to close the file, open it, read it to clipboard and close it again. I dont see how else I would be able to read back a file that I have opened to write to (bear in mind the script is not doing the writing. The script so far as I can understand is halted in fact while the logfile is written to) May 1000 flowers bloom!! |
|
#2
|
|||
|
|||
|
Hi Slouw,
After analyzing your script, and what it is you want to accomplish, we think that the easiest method to get the data to the clipboard would be to open the log file after you have disabled logging, read in the data, close the log file again, and send that data to the clipboard. Here is an example that illustrates how to accomplish this: Code:
|
|
#3
|
|||
|
|||
|
Hi Todd
Thanks. That is a particular talent reading other peoples source code. I dont know which is more confusing - my code or my comments .In the mean time I experimented a little by replacing WaitForStrings with ReadString. I tested the script attached. In theory it should have worked the same but it would no longer accommodate long outputs which are paged (This is ok for now). And indeed it worked for a short output e.g. "sh version" or "sh clock". One thing happened though which I want to ask about. At the front of the clipboard output there are 2 inconsistencies. (1) There is a series of funny characters at the beginning. I am not sure I can reproduce them here. They are of the form below. (2) Line 2 is extra in the clipboard output compared to the logfile. Every thing else works fine (my logfile still works fine). I am presenting 2 examples here. One of a sh ver and one of sh clock command Screenshot of the output is included here as the characters are not properly represented with a simple paste (Why would that be? ). Text files attached as well to compare the log with the pasted output.![]() I thought it might have something to do with the format of the clipboard? I see a "format" property in the Scripting Objects reference. I am not well travelled in that area. With reagrds to the first inconsistency, I notice something also. There seems a relationship between the lenght of the command string (sh ver = 6 chars, sh clock = 8 chars) and the length of these offending stuff at the beginning of the clipboard output. New script attached. Only Section 3 has been changed. Thanks as ever for any help... Attached logs 038CA Ctrl-Enter-Show.vbs = Relevant script 2010-02-23 06h25m20s 52 TestSwitch sh ver.log = show version logfile (works good) sh ver clipboard output.txt = Paste after sh ver command. Funny cf good log above 2010-02-23 06h26m29s 52 TestSwitch sh clock.log = sh clock logfile (works good) sh clock clipboard output.txt = Paste after sh clock command. Funny cf good log above |
|
#4
|
|||
|
|||
|
Hi slouw,
Thanks for the information. The problem you are having appears to be a result of raw logging. Is the session you are using configured to perform raw logging? |
|
#5
|
|||
|
|||
|
Not raw loggin
No sir. In the event, not
![]() |
|
#6
|
|||
|
|||
|
Hi slouw,
Thanks for the screenshot. What appeared to be a raw logging problem, is apparently a problem caused by something else. After analyzing the code further, it seems as though the problem you are seeing may be a result of sending a "^x" to the remote after turning on logging. If you send backspaces manually, rather than use "^x", and turn on logging after you send the backspaces, do you see better results? Here is an example of how to send the backspaces manually: Code:
Code:
|
|
#7
|
|||
|
|||
|
Hi Todd
(1) ReadStr option I persisted with this against your advice: Thanks for the suggestions. I have tried both your suggestions (i) Manual backspace; and (ii)IgnoreEscape I was amused to see things got worse ![]() So I am revisting my fundamental approach now... (3) Read back the contents of the logfile at the end This is what you suggested in the first place. I did try this but this is the problem I hit upon as follows. My Log Filename setup is as follows: ![]() As you can see I have a whole lot of time variables down to the second. By the time I come to reopen the file and read back the contents, a second or more of time has elapsed and the Log Filename setup now points to a different filename. So I need to resolve the Log Filename variable when I log the output so that I can reference the crrect file when reopening it. Did that make sense? |
|
#8
|
|||
|
|||
|
Hi slouw,
Thanks for the update. I have included an example that illustrates how to get the filename of the log file. Code:
|
|
#9
|
|||
|
|||
|
After much messing around I have taken Todd's advice and gone with reopening the file at the end and reading the contents back. This is relfected in the script attached. My current problem is I get the error shown in the screenshot. Any ideas appreciated.
The error I think relates to the fragment of code at the end of the script as shown. ![]() Code:
Last edited by jdev : 04-07-2010 at 09:07 AM. |
|
#10
|
|||
|
|||
|
Hi slouw,
It appears as though the message is telling you that the 'str' object doesn't exist. This object would only exist if you had defined it previously in your script. To resolve this problem, you just need to associate the correct object with the Clipboard object. Changing the following line of code should resolve this: From: Code:
To: Code:
Last edited by rtb : 04-08-2010 at 08:18 AM. |
|
#11
|
|||
|
|||
|
Certainly does!
Many thanks.... ![]() |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|