Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

telnet.vbs - vbscript script by ActiveXperts Software

telnet.vbs establishes a telnet session to a host and validates its response.

Use telnet.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select telnet.vbs. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


telnet.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // For more information about ActiveXperts Network Monitor and VBScript, visit
' // http://www.activexperts.com/support/network-monitor/online/vbscript/
' ///////////////////////////////////////////////////////////////////////////////

Option Explicit

' Declaration of global variables
Dim   SYSDATA, SYSEXPLANATION   ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column

' Constants - return values
Const retvalUnknown = 1         ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)

' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following lines:
' Dim bResult
' bResult = CheckTelnet( "telnet.activexperts-labs.com", 23, "library", "mypasswd", "", "library" )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"


Function CheckTelnet( strHost, numPort, strCommand1, strCommand2, strCommand3, strReceive )
' Description: 
'     Establish a telnet session to a POP3 mail server and validate its response
'     POP3 servers always reply a '+OK' message when ready to establish a session
'     This function uses ActiveXperts Network Component.
'     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased
'     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
' Parameters:
'     1) strHost - a telnet server to connect to
'     2) numPort - the port to connect to
'     3) strCommand1 - the first command to send to the telnet server
'     4) strCommand2 - the second command to send to the telnet server
'     5) strCommand3 - the third command to send to the telnet server
'     6) strReceive - the string you should receive
' Usage:
'     CheckTelnet( "<Hostname | IP>", <Port, "<Command1>", "<Command2>", "<Command3>", "<Receive string>" )
' Sample:
'     CheckTelnet( "telnet.activexperts-labs.com", 23, "library", "mypasswd", "", "library" )

  Dim objTcp, strAllResponses

  CheckTelnet           = 1   ' Default return value
  SYSDATA               = ""  ' Not used by this function
  SYSEXPLANATION        = ""  ' Set initial value

  Set objTcp = CreateObject("AxNetwork.Tcp")
  objTcp.Protocol       = 2 ' 1 means: raw, 2 means: telnet (see also ActiveXperts Network Component manual)

  objTcp.Connect strHost, numPort
  If( objTcp.LastError <> 0 Or objTcp.ConnectionState <> 3 ) Then
    CheckTelnet       = False
    SYSEXPLANATION    = "Unable to connect to [" & strHost & "]"
    objTcp.Disconnect ' can be called even when there's no connection
    Exit Function
  End If

  objTcp.Sleep( 2000 ) ' Allow some time

  tcprecv objTcp, strAllResponses 

  tcpsend objTcp, strCommand1
  tcprecv objTcp, strAllResponses

  tcpsend objTcp, strCommand2
  tcprecv objTcp, strAllResponses

  tcpsend objTcp, strCommand3
  tcprecv objTcp, strAllResponses

  If( InStr( UCase( strAllResponses ), UCase( strReceive )  ) <> 0 ) Then
    CheckTelnet       = True
  Else
    CheckTelnet       = False
  End If
  SYSEXPLANATION      = "Response=[" & strAllResponses & "]"

  objTcp.Disconnect    

End Function


' //////////////////////////////////////////////////////////////////////////////
' // --- Private Functions section ---
' // Private functions names should start with a lower case character, so they 
' // will not be listed in the Network Monitor's function browser.
' //////////////////////////////////////////////////////////////////////////////

Sub tcpsend( objTcp, strCommand )
  If( strCommand <> "" ) Then
    objTcp.SendString( strCommand )
  End If
End Sub

' //////////////////////////////////////////////////////////////////////////////

Sub tcprecv( objTcp, BYREF strAllResponses )
  Dim bHasData, strResponse 

  bHasData = objTcp.HasData

  While( bHasData )
    strResponse = objTcp.ReceiveString()
    bHasData = objTcp.HasData

    If( strAllResponses <> "" ) Then
      strAllResponses = strAllResponses & ".." ' .. to indicate a newline
    End If
    strAllResponses = strAllResponses & strResponse
  WEnd
End Sub