Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-11-2007, 06:16 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
Secure CRT script

Is it possible to write a VBS script that runs in secure crt, and have the script determine what my IP address is, at the time the script is run in order to do database backups?
  #2  
Old 09-11-2007, 08:34 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Yes. You could run "ipconfig" from a shell object then parse out the IP address. Another option might be to ping a third party to find out your IP address, but this usually only shows the address of your firewall. Will either of these methods work for you?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
  #3  
Old 09-11-2007, 09:24 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
Secure CRT script

yes running the ipconfig would work, but my IP changes on a weekly basis, so I want to have something within the script that would open a shell and do an IPconfig. then tell the script to take that IP address and use it as tftp server in order to pull the database down and save it. here is an example of the script with usernames, PSWDS, and IP changed. I know that the equipment names are the same for somee but that is bc i took out most of the names, in real life they are different. 172.26.55.196 is the address of the tftp server, in this case it would be my desktop and this is where i want the script to look for my IP address.

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

Option Explicit
Dim navuser
Dim navpswd
Dim navsite
Dim addr

navuser="na"
navpswd="na"

Sub Main

navsite="""01"""
addr="172.26.130.133"
call work (navuser, navpswd, navsite, addr)

navsite="""02"""
addr="172.26.130.134"
call work (navuser, navpswd, navsite, addr)

navsite="""01"""
addr="172.26.130.166"
call work (navuser, navpswd, navsite, addr)

navsite="""02"""
addr="172.26.130.167"
call work (navuser, navpswd, navsite, addr)

navsite="""01"""
addr="172.26.130.240"
call work (navuser, navpswd, navsite, addr)

' navsite="""02"""
' addr="172.26.130.241"
' call work (navuser, navpswd, navsite, addr)

navsite="""01"""
addr="172.26.130.234"
call work (navuser, navpswd, navsite, addr)

navsite="""t02"""
addr="172.26.130.235"
call work (navuser, navpswd, navsite, addr)

navsite="""3"""
addr="172.26.130.238"
call work (navuser, navpswd, navsite, addr)

navsite="""v04"""
addr="172.26.130.220"
call work (navuser, navpswd, navsite, addr)

navsite="""05"""
addr="172.26.130.216"
call work (navuser, navpswd, navsite, addr)

navsite="""06"""
addr="172.26.130.207"
call work (navuser, navpswd, navsite, addr)

End Sub

Sub work (navuser, navpswd, navsite, addr)

crt.Session.Connect "/TELNET " & addr
crt.sleep 1000
crt.Screen.Send vbCr
crt.Screen.WaitForString "Login: "
crt.Screen.Send navuser & vbCr
crt.Screen.WaitForString "Password: "
crt.Screen.Send navpswd & vbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "upload tftp config 172.26.55.196 " & navsite & vbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "exit" & vbCr
crt.sleep 1000

End Sub


' 01 172.26.130.133
' 02 172.26.130.134

' 01 172.26.130.166
' 02 172.26.130.167

' 01 172.26.130.240
' 02 172.26.130.241

' 01 172.26.130.234
' 02 172.26.130.235
' 03 172.26.130.238
' 04 172.26.130.220
' 05 172.26.130.216
' 06 172.26.130.207
  #4  
Old 09-11-2007, 09:27 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
Secure CRT script

PS, where is a good place to get material on VBS script writing, kinda like a VBS for dummies?
  #5  
Old 09-11-2007, 10:09 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
I'm working on a simple example script that uses IPconfig and parses the data. I'll post the example when it's finished. There are many VBScript books available but most discuss VBScript from a web development perspective. The best references I've found are the language reference and the user guide. You can find both online here:
http://msdn2.microsoft.com/en-us/library/t0aew7h6.aspx
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
  #6  
Old 09-11-2007, 10:26 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
After beginning this script I've realized that ipconfig might be problematic if you have multiple network interfaces.

Do you have multiple network interfaces?

How do you know which line to choose from ipconfig?

For example, do you always choose the IP address that follows "Ethernet adapter Local Area Connection"?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
  #7  
Old 09-11-2007, 11:09 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
VanDyke Customer Support

Mike,
yes that is the one would choose. Thanks for all the help.
  #8  
Old 09-11-2007, 11:35 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
VBS tutorial

I need a starting point for learning, about how to start a script and what lines are a necessity, any idea??
  #9  
Old 09-11-2007, 11:36 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
It might be easier to parse "ping" output rather than ipconfig output. If you type the following command does it return the correct IP address?
ping -n 1 -w 100 %computername%
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
  #10  
Old 09-11-2007, 11:45 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
Parse

yes it returned the correct IP
  #11  
Old 09-11-2007, 12:38 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
The example code below will popup a message box showing your computer's IP address. It currently ready for use within SecureCRT, but could be run outside of SecureCRT by uncommenting the first two lines.

Code:
'~ If running this script outside of SecureCRT, uncomment the following lines
'Set g_Shell = WScript.CreateObject("WScript.Shell")
'Main

'~ If running this script from within SecureCRT, uncomment the following line
Set g_Shell = CreateObject("WScript.Shell") 

Sub Main

    IPaddress = g_Shell.Run("cmd /c ipconfig")

    strCmdString = "ping -n 1 -w 100 %computername%"

    set objWshShell = CreateObject("WScript.Shell")

    set objExec = objWshShell.Exec(strCmdString)
    
    do while not objExec.StdOut.AtEndOfStream
        szOutput = szOutput & objExec.StdOut.Readline()
    loop

    szIPaddr = ParseIPfromPing(szOutput)

    If szIPaddr = "" Then
        MsgBox "Unable to find IP address from ping output"
        Exit Sub
    End If

    '~ msgbox szOutput
    msgbox szIPaddr

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~ ParseIPfromPing
'~ Parses output from the following command:
'~ ping %computername% -n 1 -w 100
'~ Pinging machine.domain.org [10.1.1.102] with 32 bytes of data:
'~ ...

Function ParseIPfromPing(strng)   
    Dim regEx, Match, Matches   ' Create variable.
    Set regEx = New RegExp   ' Create regular expression.
    regEx.Pattern = "\[(.*)\]"   ' Set pattern.
    regEx.IgnoreCase = False   ' Set case insensitivity.
    regEx.Global = True   ' Set global applicability.
    
    If Not regEx.Test(strng) Then
        Exit Function
    End If
    
    Set Matches = regEx.Execute(strng)   ' Execute search.
    
    For Each Match in Matches 
        ParseIPfromPing = Match.Submatches(0)
        Exit Function    
    Next
    
End Function
Does this script help you get the IP address?


Regarding your question about a good starting point, example scripts are often a very useful learning tool. There are many good websites that can help with VBScript questions. The following URL is one of many good VBScript resources. It contains links to samples, a language reference, a tutorial, and much more.
http://www.programmershelp.co.uk/vbscriptlinks.php
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
  #12  
Old 09-11-2007, 12:58 PM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
reply

Mike,
This looks like it would help, would I just paste that at the beginning of my script and run the script? Thanks for the link
  #13  
Old 09-11-2007, 03:39 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Yes, you can add it to the beginning of your script. More specifically, it looks to me like everything within Main of the example script I provided could be pasted to the beginning of your Main. The Function ParseIPfromPing(strng) should be pasted outside of Main, as in the example.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
  #14  
Old 09-12-2007, 07:59 AM
jujordan jujordan is offline
Registered User
 
Join Date: Sep 2007
Posts: 23
Crt

Mike,
here is my script, thanks for all the help. 2 small problems, after the IP is determined, there needs to be a carriage return to allow the pop up box to disappear and continue the script. Last it is not putting my IP address of my computer in the line crt.Screen.Send "upload tftp config 172.26.55.196 " & navsite & vbCr. This is critical becuase it is telling the script where to save the file, the 172.26.55.196, should be replaced with my IP, what are your thoughts?



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


'Code:
'~ If running this script outside of SecureCRT, uncomment the following lines
'Set g_Shell = WScript.CreateObject("WScript.Shell")
'Main

'~ If running this script from within SecureCRT, uncomment the following line
Set g_Shell = CreateObject("WScript.Shell")

Option Explicit
Dim navuser
Dim navpswd
Dim navsite
Dim addr

navuser="na"
navpswd="na"


Sub Main

IPaddress = g_Shell.Run("cmd /c ipconfig")

strCmdString = "ping -n 1 -w 100 %computername%"

set objWshShell = CreateObject("WScript.Shell")

set objExec = objWshShell.Exec(strCmdString)

do while not objExec.StdOut.AtEndOfStream
szOutput = szOutput & objExec.StdOut.Readline()
loop

szIPaddr = ParseIPfromPing(szOutput)

If szIPaddr = "" Then
MsgBox "Unable to find IP address from ping output"
Exit Sub


End If

'~ msgbox szOutput

msgbox szIPaddr


navsite="""01"""
addr="172.26.130.133"
call work (navuser, navpswd, navsite, addr)

navsite="""02"""
addr="172.26.130.134"
call work (navuser, navpswd, navsite, addr)

navsite="""01"""
addr="172.26.130.166"
call work (navuser, navpswd, navsite, addr)

navsite="""02"""
addr="172.26.130.167"
call work (navuser, navpswd, navsite, addr)

navsite="""01"""
addr="172.26.130.240"
call work (navuser, navpswd, navsite, addr)

' navsite="""02"""
' addr="172.26.130.241"
' call work (navuser, navpswd, navsite, addr)

navsite="""01"""
addr="172.26.130.234"
call work (navuser, navpswd, navsite, addr)

navsite="""t02"""
addr="172.26.130.235"
call work (navuser, navpswd, navsite, addr)

navsite="""3"""
addr="172.26.130.238"
call work (navuser, navpswd, navsite, addr)

navsite="""v04"""
addr="172.26.130.220"
call work (navuser, navpswd, navsite, addr)

navsite="""05"""
addr="172.26.130.216"
call work (navuser, navpswd, navsite, addr)

navsite="""06"""
addr="172.26.130.207"
call work (navuser, navpswd, navsite, addr)

End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~ ParseIPfromPing
'~ Parses output from the following command:
'~ ping %computername% -n 1 -w 100
'~ Pinging machine.domain.org [10.1.1.102] with 32 bytes of data:
'~ ...

Function ParseIPfromPing(strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = "\[(.*)\]" ' Set pattern.
regEx.IgnoreCase = False ' Set case insensitivity.
regEx.Global = True ' Set global applicability.

If Not regEx.Test(strng) Then
Exit Function
End If

Set Matches = regEx.Execute(strng) ' Execute search.

For Each Match in Matches
ParseIPfromPing = Match.Submatches(0)
Exit Function
Next

End Function


Sub work (navuser, navpswd, navsite, addr)

crt.Session.Connect "/TELNET " & addr
crt.sleep 1000
crt.Screen.Send vbCr
crt.Screen.WaitForString "Login: "
crt.Screen.Send navuser & vbCr
crt.Screen.WaitForString "Password: "
crt.Screen.Send navpswd & vbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "upload tftp config 172.26.55.196 " & navsite & vbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "exit" & vbCr
crt.sleep 1000

End Sub


' 01 172.26.130.133
' 02 172.26.130.134

' 01 172.26.130.166
' 02 172.26.130.167

' 01 172.26.130.240
' 02 172.26.130.241

' 01 172.26.130.234
' 02 172.26.130.235
' 03 172.26.130.238
' 04 172.26.130.220
' 05 172.26.130.216
' 06 172.26.130.207
  #15  
Old 09-12-2007, 08:29 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
needs to be a carriage return to allow the pop up box to disappear and continue the script
Remove the msgbox szIPaddr line if you don't want the popup window.

Quote:
Last it is not putting my IP address of my computer in the line crt.Screen.Send "upload tftp config 172.26.55.196 " & navsite & vbCr.
I think you need to pass szIPaddr to the function if you want your IP address. You have "upload tftp config 172.26.55.196 " & navsite & vbCr in your work function. It seems like you found a function that works with hard coded values, but the function could also work with parameters passed in. When you call Work(), try passing szIPaddr as the address (4th parameter). Use this parameter within the function. More specifically, I think this is what you could use to accomplish your goal:

call work (navuser, navpswd, navsite, szIPaddr)
'... work function...
crt.Screen.Send "upload tftp config " & addr & navsite & vbCr
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 04:59 PM.