Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-03-2013, 09:56 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Error connection logged in txt file

Hi All,
in reference to the part of the following script:

Code:
...I have a lot of ip address and I must telnet everything.....(the script with the cycle of connection works)

				For Each strHost In THost 
 				If strHost = "" Then Exit For
 					On Error Resume next
					If crt.Session.Connected Then
       	 		Do 
      			crt.Session.Disconnect
          	Loop While crt.Session.Connected
          end if
How can I have a txt file with the name(or the ip) of the device that fails the connection? Is it possible?

Thank you!
Reply With Quote
  #2  
Old 04-03-2013, 10:22 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi hakelo91,

This topic has been discussed previously in these forums. We also have an example posted in the scripting forum sticky that illustrates how to log data to a file using the VBScript FileSystemObject. Here is where you can get a copy of the example script:
http://forums.vandyke.com/showthread...7294#post37294
Line 20 shows how to get a handle to the FileSystemObject.

Lines 284 to 295 show how to use the object handle to open a file, write data to the file and close the file.

If you are not familiar with using VBScript , Microsoft provides documentation at the following location:
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
Here is the specific location for help with the FileSystemObject:
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
Disclaimer: The above URLs may not remain valid forever because Microsoft changes their website locations periodically. You should be able to locate the current VBScript language reference using a web search for the following terms:
vbscript language reference


Does this help you add the desired logging to your script?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 04-05-2013, 09:07 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,

This topic has been discussed previously in these forums. We also have an example posted in the scripting forum sticky that illustrates how to log data to a file using the VBScript FileSystemObject. Here is where you can get a copy of the example script:
http://forums.vandyke.com/showthread...7294#post37294
Line 20 shows how to get a handle to the FileSystemObject.

Lines 284 to 295 show how to use the object handle to open a file, write data to the file and close the file.

If you are not familiar with using VBScript , Microsoft provides documentation at the following location:
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
Here is the specific location for help with the FileSystemObject:
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
Disclaimer: The above URLs may not remain valid forever because Microsoft changes their website locations periodically. You should be able to locate the current VBScript language reference using a web search for the following terms:
vbscript language reference


Does this help you add the desired logging to your script?

Thank you for the answer.

mmm...I don't know very well the vsb language.

I try to read the example script but I'm very confused.

I add
Code:
Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")
at the beginning of my script.

Then I add
Code:
Set objFile = g_fso.OpenTextFile(strLogFile, ForAppending, True)

                    ' If you want the command logged along with the results,
                    ' uncomment the next two lines
                    objFile.WriteLine "Results of command """ & strCommand & _
                        """ sent to host """ & strHost & """: "

                    ' Write out the results of the command and a separator
                    objFile.WriteLine strResult
                    
                    ' Close the log file
                    objFile.Close
before the "next" command of the cycle.

But....I don't know what I'm doing and it doesn't work.

Can I have an extra help?
Reply With Quote
  #4  
Old 04-05-2013, 04:35 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi hakelo91,
Quote:
But....I don't know what I'm doing and it doesn't work.
How is your script failing?

What specific behavior are you seeing?

Would you be able to post your script now that you have modified it?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 04-08-2013, 04:32 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,

How is your script failing?

What specific behavior are you seeing?

Would you be able to post your script now that you have modified it?
Yes, this is my script:

Code:
# $language = "VBScript"
# $interface = "1.0"

crt.Screen.Synchronous = True

Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")

.....................

select case scelta
			case "1"
				.....login string......
				
					
				For Each strHost In THost 
 				If strHost = "" Then Exit For
 					On Error Resume next
					If crt.Session.Connected Then
       	 		Do 
      			crt.Session.Disconnect
          	Loop While crt.Session.Connected
          end if
      	
      	crt.Screen.Synchronous = True
 				crt.Session.Connect "/telnet " & strHost 
 				crt.Screen.WaitForString "Username: "
  			crt.Screen.Send xxx & chr(13)
				crt.Screen.WaitForString "Password: "
				crt.Screen.Send yyy & chr(13)
  			crt.Screen.WaitForString "#"
				crt.Screen.Send "sh run" & chr(13)
				crt.Screen.Send  chr(13)
				crt.Screen.Send  chr(13)
				crt.Screen.WaitForString "#"
				Set objFile = g_fso.OpenTextFile(strLogFile, ForAppending, True)

                    ' If you want the command logged along with the results,
                    ' uncomment the next two lines
                    objFile.WriteLine "Results of command """ & strCommand & _
                        """ sent to host """ & strHost & """: "

                    ' Write out the results of the command and a separator
                    objFile.WriteLine strResult
                    
                    ' Close the log file
                    objFile.Close
				next
			case "2"

......ect
I reported only the important part of the script....

When it open a log txt file? Where is it located?
I tried to connect specifically to a device with a different login (so the session respond with failed authentication) but it skip this device and continue to the cycle of ip.
Help please!
Reply With Quote
  #6  
Old 04-08-2013, 11:23 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi Hakelo91,

Line 36 of the code that you posted is where the file is opened:
Code:
Set objFile = g_fso.OpenTextFile(strLogFile, ForAppending, True)
The function takes a few parameters as defined in the Microsoft VBScript help that I pointed you to earlier.

Please note that the first parameter in the example is a variable and the second is a constant, and both must be defined prior to executing the line of code. I don't see that you have done this. Here is a list of the variables in the example code that I provided which will need to be defined before it will successfully execute:
strLogFile
strCommand
strResult
ForAppending is actually a constant, and I didn't add the code from the example that I posted, so here it is:
Code:
' Constants used for reading and writing files.
Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8
You define the path of the file to use for logging by setting strLogFile equal to a valid path on your machine. For example:
Code:
strLogFile = "C:\Temp\Logs\LogFile.txt"
After you define all of the variables and constants, are you able to successfully log your data?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 04-10-2013, 04:30 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Wow! You gave me a great help! Thank you! I begin to understand...
Some questions:
In the log file now I find:
Code:
Results of command "" sent to host "xxx.xxx.xxx.xxx": 

Results of command "" sent to host "xxx.xxx.xxx.xxx":
How can I have in the log file for example:
Code:
Results of connection to host "xxx.xxx.xxx.xxx":OK or 
Results of connection to host "xxx.xxx.xxx.xxx":Failed

Results of command sh run to host "xxx.xxx.xxx.xxx": OK or 
Results of command sh run to host "xxx.xxx.xxx.xxx":Failed
?
Thank you a lot!
Reply With Quote
  #8  
Old 04-10-2013, 10:22 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi hakelo91,

To log whether a connection is successful, you could use a conditional statement based on whether you are connected. For example:
Code:
If crt.Session.Connected Then
    ' write out success to log
Else
    ' write out failure to log
End If
With regards to logging whether a command is successful or not, how do you determine this if you are running the command manually?

Based on the output in your log file, it still looks like you have not defined the variable strCommand. Would you post the current iteration of your script?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #9  
Old 04-11-2013, 07:59 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Quote:
Originally Posted by rtb View Post
Hi hakelo91,

To log whether a connection is successful, you could use a conditional statement based on whether you are connected. For example:
Code:
If crt.Session.Connected Then
    ' write out success to log
Else
    ' write out failure to log
End If
With regards to logging whether a command is successful or not, how do you determine this if you are running the command manually?

Based on the output in your log file, it still looks like you have not defined the variable strCommand. Would you post the current iteration of your script?
Hi rtb,
the part of successful connection control is OK! Thank you it works!
Respect to second part.....
I determine that the command has been executed correctly if the session responds with "Building configuration". Is there a condition that if the device responds with "Building configuration" it logs with "command successful" else it logs with "command failed"?

On the top of the script I declared the variable "strCommand" (I think ):

Code:
# $language = "VBScript"
# $interface = "1.0"

crt.Screen.Synchronous = True

Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")
dim strLogFile
dim strCommand
dim strResult
' Constants used for reading and writing files.
Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8
strLogFile = "C:\Logvsb\Log.txt"
Thank you so much.
Reply With Quote
  #10  
Old 04-11-2013, 02:57 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi hakelo91,
Code:
dim strCommand
This is a variable declaration. It is not a variable assignment or definition. The following would be defining a variable:
Code:
strCommand = "sh run"
To accomplish your goal of knowing whether the command was successful, you would use the same pattern I provided in my previous post. For example:
Code:
If crt.Session.Connected Then
    ' write out success to log
Else
    ' write out failure to log
End If
The difference would be the condition. For example, you could read in the data between issuing the command and the prompt indicating completion, and see if you find the string you want. Here is how that might look:
Code:
strResult = crt.Screen.ReadString("#")
If Instr(strResult, "Building configuration") Then
    ' write out success to log
Else
    ' write out failure to log
End If
Does this help?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #11  
Old 04-12-2013, 05:42 AM
hakelo91 hakelo91 is offline
Registered User
 
Join Date: Mar 2013
Posts: 13
Thank you!!! You're the best! It works and I resolved my problem!
Thank you so much!
Reply With Quote
  #12  
Old 04-12-2013, 08:42 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Congratulations hakelo91!

I am glad to hear that you have successfully written a script to accomplish your goal. I am also happy to have been of assistance.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 08:17 AM.