Non-interactive SSH Session

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
TheEek
Junior Coder
Posts: 31
Joined: Mon Jun 22, 2009 2:53 pm

Non-interactive SSH Session

Post by TheEek » Thu Apr 01, 2010 7:38 am

I've seen a few examples of Macro Scheduler scripts using SSH to connect to remote systems, but I'm really after something that doesn't require an interactive session. I have some of this worked out using VBScript, but I need some help with this please? The following can be found at http://www.example-code.com/vbscript/ssh_exec.asp (I've included the code below in case you don't like external links). I entered the code between the "VBSTART" and "VBEND" tags, and it worked quite nicely. However, I need a little more control in this, need to add a "wait" command or 2, for the remote station to catch up etc… I'm not sure how to enter the VBScript sleep command, I always get errors.

Code: Select all

Wscript.sleep [time in milliseconds]
produces runtime errors.

Code: Select all

vbstart
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

set ssh = CreateObject("Chilkat.Ssh")
success = ssh.UnlockComponent("Anything for 30-day trial")
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

hostname = "192.168.1.108"
port = 22
success = ssh.Connect(hostname,port)
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

ssh.IdleTimeoutMs = 5000

success = ssh.AuthenticatePw("myLogin","myPassword")
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

channelNum = ssh.OpenSessionChannel()
If (channelNum < 0) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

success = ssh.SendReqExec(channelNum,"ls -la")
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

pollTimeoutMs = 2000
n = ssh.ChannelReadAndPoll(channelNum,pollTimeoutMs)
If (n < 0) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

success = ssh.ChannelSendClose(channelNum)
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

success = ssh.ChannelReceiveToClose(channelNum)
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

cmdOutput = ssh.GetReceivedText(channelNum,"ansi")
If (cmdOutput = vbNullString ) Then
    MsgBox ssh.LastErrorText
    WScript.Quit
End If

outFile.WriteLine(cmdOutput)
ssh.Disconnect 
outFile.Close

VBEND
I tried to split this code (logically just after the "ls -la" command) at least that gave me a diferent error. Any one have any ideas?

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

V12 now supports Wscript.sleep in VBScript

Post by adroege » Wed May 19, 2010 2:05 pm

Macro Scheduler 12 ships with a built-in WScript object which implements the Echo and Sleep functions. This means that Wscript.Echo and WScript.Sleep functions will continue to work and will not need to be removed to make the .vbs work in Macro Scheduler.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts