Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-06-2007, 01:51 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
Automate ssh login from one system to another

Hi,

I am running SecureCRT v5.0.0 (Build 992) and wanted to find out how I could script doing a ssh to a unix server with a hardcoded username and password, and then from that server telnet to a remote device. The only input parameter would be the hostname/ipaddress of the remote device. Any help would be very appreciated.
Reply With Quote
  #2  
Old 07-06-2007, 06:10 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi,

One way to do this would be to call a script with a command line such as this:
securecrt /s server1 /script c:\ssh-to-arg.vbs /arg server2
The ssh-to-arg.vbs script could be as simple as this:
Code:
Crt.Screen.Send "ssh " & crt.Arguments.GetArg(0) & vbcr
Would this help you?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 07-12-2007, 08:48 AM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
Still not working

Hi Mike,

Thanks for the response but it does not seem to work. I get a pop-up window with the error - <hostname> was not found in the session database. Any help would be very appreciated.

Thanks
Reply With Quote
  #4  
Old 07-12-2007, 10:23 AM
tnygren's Avatar
tnygren tnygren is offline
Registered User
 
Join Date: May 2005
Posts: 1,408
Hi 1720,

The sample command that Mike provided is calling a session (server1) to start the connection.

The error indicates that SecureCRT could not find the session name specified in the command line.

Does a session with the name that was specified after the '/S', exist in the session database?

If so, does the session appear inside a folder when viewed in the 'Connect' dialog in SecureCRT?
__________________
Thanks,

Teresa

Teresa Nygren
Reply With Quote
  #5  
Old 07-12-2007, 12:21 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
Hi Teresa,

No there is no session saved with that name. What I am trying to do is to ssh to my server with a known username/password. And from there, telnet or ssh to a second device where the second device is passed as an argument and the second set of username/password are prompted. So far I have the following -

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

' Connect to an SSH server using the SSH2 protocol. Specify the
' username and password and hostname on the command line as well as
' some SSH2 protocol specific options.

Sub Main

Dim host
host = "10.20.4.205"
Dim user
user = "engineer"
Dim router_username
router_username = crt.Dialog.Prompt("Enter your TACACS ID for ", & host "TACACS Login")
Dim passwd
passwd = "mypasswd"

' Build a command-line string to pass to the Connect method.
'
cmd = "/SSH2 /L " & user & " /PASSWORD " & passwd & " /C 3DES /M MD5 " & host

crt.Session.Connect cmd

crt.Screen.Send "ssh -l " & router_username " pcpf11sb.smart.net" & chr(13)
crt.Screen.WaitForString "Are you sure you want to continue connecting (yes/no)? "
crt.Screen.Send "yes" & chr(13)
crt.Screen.WaitForString & router_username "@pcpf11sb.smart.net's password: "
' Prompt for a password instead of embedding it in a script...
'
'Dim passwd_device
'passwd_device = crt.Dialog.Prompt("Enter password for " pcpf11sb, "Login", "",True)
crt.Screen.Send "newyork1" & chr(13)


End Sub
Reply With Quote
  #6  
Old 07-12-2007, 01:01 PM
tnygren's Avatar
tnygren tnygren is offline
Registered User
 
Join Date: May 2005
Posts: 1,408
Hi 1720,

That does explain the error message.

In this case, it may be better to pass the arguments to the script and then use them in the script.

A simple example of a script that will message box the arguments passed to it would be:

Code:
Main

Sub Main()
    If WScript.Arguments.Count > 0 then
        For nIndex = 1 to WScript.Arguments.Count
            szArgs = szArgs & vbcrlf & "Arg #" & nIndex & ": " & WScript.Arguments(nIndex - 1)
        Next
    else
        szArgs = "(No args specified for this script)"
    end if
    MsgBox "Arguments sent to this script include: " & vbcrlf & szArgs
End Sub
Since the arguments are already in the Arguments array, these could be used to create the ssh command line.

Since the arguments would contain several parts of the ssh command, he arguments would need to be entered in the same order each time. If they weren't, then there would be issues constructing the command line.

As this is a VBScript option, more information on the Arguments array can be found on Microsoft's scripting help pages.

Would this be an option that could help?
__________________
Thanks,

Teresa

Teresa Nygren
Reply With Quote
  #7  
Old 07-16-2007, 10:10 AM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
Hi Teresa,

The script is almost working except it is not accepting command line args. So here is what I am doing from cmd line -

securecrt /script .\scripts\ssh.vbs /arg test

I get the following error in a popup window -
Error: Object required: 'WScript'

Here is my updated script -
# $language = "VBScript"
# $interface = "1.0"

' Connect to an SSH server using the SSH2 protocol. Specify the
' username and password and hostname on the command line as well as
' some SSH2 protocol specific options.

Sub Main

Dim host
host = "10.20.4.205"

Dim user
user = "engineer"

Dim passwd
passwd = "mypasswd"

Dim device
device = WScript.Argument(0)

Dim user_name
user_name = crt.Dialog.Prompt("Enter your TACACS ID for ", user_name, "TACACS Login",False)

' Build a command-line string to pass to the Connect method.
cmd = "/SSH2 /L " & user & " /PASSWORD " & passwd & " /C 3DES /M MD5 " & host
crt.Session.Connect cmd

crt.Screen.Send "ssh -l " & user_name & " " & device & chr(13)
crt.Screen.WaitForString "Are you sure you want to continue connecting (yes/no)? "
crt.Screen.Send "yes" & chr(13)

End Sub
Reply With Quote
  #8  
Old 07-16-2007, 02:45 PM
tnygren's Avatar
tnygren tnygren is offline
Registered User
 
Join Date: May 2005
Posts: 1,408
Hi 1720,

I think that I may have miss understood how the script is being run.

If SecureCRT is used to start the script, then it is the script object host is SecureCRT not wscript. The wscript.arguments object will not be available but crt.arguments will be available.

If the following line:

device = WScript.Argument(0)

Is changed to:

device = crt.Arguments(0)

Does the script work correctly?
__________________
Thanks,

Teresa

Teresa Nygren
Reply With Quote
Reply


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 03:29 PM.