Welcome to the VanDyke Software Forums

Join the discussion today!

Go Back   VanDyke Software Forums > Scripting

Thread Tools Rate Thread Display Modes
Old 12-16-2004, 11:27 AM
adeshotel adeshotel is offline
Registered User
Join Date: Dec 2004
Location: Houston, TX
Posts: 14
Reading the Hostname with a script

With a script that has successfully achieve a connection to a device, in my case Cisco, how do I get it to read the characters of the host name?

The prompt would be something like:

Is there a get.Line type function that could be used to read the current line of characters or do I have to figure out the positon of the cursor and read from column1 to the current column using the current line?

Reply With Quote
Old 12-16-2004, 11:46 AM
okstorms's Avatar
okstorms okstorms is offline
Registered User
Join Date: Aug 2004
Location: Wilmington, DE
Posts: 38
crt.Screen.Get(crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, 80)
That command will get the current row from column 1 to column 80, which should be enough in most cases, but you may have a session that is longer then 80 columns so that may need modification.

From there all you need to do is take all the characters left of the "#" character.

prompt = crt.Screen.Get(crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, 80)
router = Left(prompt, InStr(prompt, "#") - 1)

Last edited by okstorms; 12-16-2004 at 12:07 PM.
Reply With Quote
Old 12-16-2004, 12:21 PM
adeshotel adeshotel is offline
Registered User
Join Date: Dec 2004
Location: Houston, TX
Posts: 14
hostname = crt.Screen.Get(crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, (crt.Screen.CurrentColumn -2))

Yeah I was sort of afraid it wouldn't be as clean as I had hoped for. The above line is what I ended up using. Provided no one has messed with the prompting and left the IOS device using the > or # behind the host name this works.

Thanks for the kickstart!

Reply With Quote
Old 12-05-2017, 03:29 AM
itzvinodh itzvinodh is offline
Registered User
Join Date: Nov 2017
Posts: 3
Get hostname from current session

Is there any statment equalent to reading hostname from current session.

Vinodh G R
Reply With Quote
Old 12-05-2017, 08:33 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
Join Date: Jan 2017
Posts: 654
Hi itzvinodh,

You may be able to get the information you're wanting by using the GetOption method. I've included an example below.
# $language = "VBScript"
# $interface = "1.0"

Set objTab = crt.GetScriptTab
Set objConfig = objTab.Session.Config
strHostname = objConfig.GetOption("Hostname")

crt.Dialog.MessageBox strHostname
Does this provide the behavior you're looking for?

VanDyke Software
Technical Support
(505) 332-5730
Reply With Quote
Old 12-06-2017, 07:45 AM
thera34's Avatar
thera34 thera34 is offline
Registered User
Join Date: May 2015
Location: Romania
Posts: 8
Hello Gents,

I have found a while ago a pretty nice function in VB which I used it successfully in my scripts to determine the device's hostname.

Not my creation, but on the site/forum (can't recall exactly where, maybe on this very forum), the guy who wrote it also shared it for other users

Hope it will suit your needs.

' -----------------------------------------------------------------------------
' GetShellPrompt - determine the shell's prompt
' -----------------------------------------------------------------------------
Function GetShellPrompt()
																				' Heuristically determine the shell's prompt.  Crt.Screen.Synchronous must
																				' already have been set to True.  In general, Crt.Screen.Synchronous should
																				' be set to True immediately after a successful crt.Session.Connect().  In
																				' This script, SecureCRT should already be connected -- otherwise, a script
																				' error will occur.
    Do																			' Simulate pressing "Enter" so the prompt appears again...
        crt.Screen.Send vbcr
																				' Attempt to detect the command prompt heuristically by waiting for the
																				' cursor to stop moving... (the timeout for WaitForCursor above might
																				' not be enough for slower- responding hosts, so you will need to adjust
																				' the timeout value above to meet your system's specific timing
																				' requirements).
            bCursorMoved = crt.Screen.WaitForCursor(1)
        Loop Until bCursorMoved = False
																				' Once the cursor has stopped moving for about a second, it's assumed
																				' it's safe to start interacting with the remote system. Get the shell
																				' prompt so that it's known what to look for when determining if the
																				' command is completed. Won't work if the prompt is dynamic (e.g.,
																				' changes according to current working folder, etc.)
        nRow = crt.Screen.CurrentRow
        strPrompt = crt.Screen.Get(nRow, 0, nRow, crt.Screen.CurrentColumn - 1)
																				' Loop until a line of non-whitespace text actually appears:
        strPrompt = Trim(strPrompt)
        If strPrompt <> "" Then Exit Do
    GetShellPrompt = strPrompt
End Function
Best Regards,
Reply With Quote
Old 04-14-2019, 05:42 PM
Elisha Elisha is offline
Registered User
Join Date: Dec 2018
Posts: 2
Hostname on WaitForString

Hi Ekoranyi
My VB like this, i want get host name and used it on "waitforstring", as you know Cisco "show tech" contains many "#"s, I can't use it as an end match.

My Scripting as below, would you mind tell me how to get hostname and used it on WaitforString. thanks!

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

crt.Screen.Synchronous = True

' This automatically generated script may need to be
' edited in order to work correctly.

Sub Main
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\Users\Elisha\Documents\secure crt\host.txt")
crt.Window.Show 3
Do Until file.AtEndOfStream
line = file.ReadLine
commendAndLog line

End Sub

Sub commendAndLog(line)
equipmentInfo = Split(line)
ip = equipmentInfo(0)
uname = equipmentInfo(1)
pwd = equipmentInfo(2)
equip_type = equipmentInfo(3)
Call crt.Session.Log(False)
crt.session.ConnectInTab "/SSH2 /ACCEPTHOSTKEYS /L "&uname&" /PASSWORD "&pwd&" "&ip
crt.Session.LogFileName = "C:\Users\Elisha\Documents\secure crt\a_log\%H.txt"
Call crt.Session.Log(True)
crt.Screen.Synchronous = True
select case equip_type
case "Cisco"
call Cisco_device
crt.screen.WaitForString ">"
case Else
msgbox "Data Error"
end select
'crt.screen.WaitForString ">"
Call crt.Session.Log(False)
End Sub

Sub Cisco_device()
crt.screen.WaitForString ">"
crt.Screen.Send "terminal length 0" & chr(13)
crt.screen.WaitForString ">"
crt.Screen.Send "en" & chr(13)
crt.screen.WaitForString "Password:"
crt.Screen.Send 1234567" & chr(13)
crt.screen.WaitForString "#"
crt.Screen.Send vbcr
crt.Screen.WaitForString vbcr
crt.Screen.Send "show version" & chr(13)
crt.screen.WaitForString "#"
crt.Screen.Send "show env all" & chr(13)
crt.screen.WaitForString "#"
crt.Screen.Send "show inventory" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "show version" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "show processes cpu sort" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "show processes memory sort" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "dir" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "show log" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "show running" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "show tech-support" & chr(13)
crt.Screen.WaitForString "hostname"
crt.Screen.Send "disable" & chr(13)
crt.Screen.WaitForString ">"
crt.Screen.Send chr(13)
End Sub
Reply With Quote
Old 04-15-2019, 08:20 AM
bgagnon bgagnon is offline
VanDyke Technical Support
Join Date: Oct 2008
Posts: 4,308
Hi Elisha,

I have only a Python example of how to get hostname presently. If there is time later in the week (and you are not able), I may be able to help you convert that to VBScript.

def GetHostname():
    crt.Screen.Synchronous = True
    # Let's get our Cisco devices 'hostname' from the running config
    strCmd = "show run | inc ^hostname\r"
    # Wait for what we sent (the actual command) to be echo'd back to us...
    crt.Screen.WaitForStrings([strCmd + "\r", strCmd + "\n"])
    # Now, let's capture everthing re receive, up to the next \r or \n:
    strHostnameData = crt.Screen.ReadString(["\r", "\n"])
    # We expect that the above command would generate output like:
    #    hostname YourHostNameHere
    if " " in strHostnameData:
        # Since output is: hostname YourHostNameHere
        # ...split the results into an array, & take the 2nd element
        #    (it's index #1):
        strHostname = strHostnameData.split()[1]
        # If we get here, there wasn't a space in the output; meaning
        # we got something unexpected.
        crt.Dialog.MessageBox("Error. Unexpected output\r\n\t" + 
            "{0}\r\n\r\nExpected: hostname YOURHOSTNAMEHERE".format(
        strHostname = "unknown-hostname"
    return strHostname
strCurHostName = GetHostname()
crt.Dialog.MessageBox("Here is the hostname we retrieved: {0}".format(

VanDyke Software
Technical Support
(505) 332-5730
Reply With Quote

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 02:46 AM.