October 22, 2008

SSH with Macro Scheduler

Filed under: Automation,Scripting — Marcus Tettmar @ 1:20 pm

As you know Macro Scheduler has Telnet, FTP and HTTP functions built in (and with v11 FTP/HTTP will support SSL). But what if you want to run some other secure protocol such as SSH?

We’ve taken the view not to reinvent the wheel and add every possible secure communications protocol to Macro Scheduler, especially as there are some great components already available that you can use in your scripts. Rather than add bulk that won’t always get used, I’d prefer to leave the security stuff to the experts and use a plug-in component when needed, such as one of the Active-X components from WeOnlyDo Software.

WeOnlyDo make Active-X components for all kinds of things, such as SSH and SFTP as well as the more general protocols Telnet, POP3, SMTP, amongst others. I recently downloaded their SSH component to help out a customer who needed a macro to access an SSH server, and was impressed with how easy it was to implement.

You can download the SSH component here.

Here’s an example I put together which logs into an SSH server, changes directory to /usr/bin and performs a directory listing, returning and displaying the results:

VBSTART
Function SSHExample(server,username,password)
  Dim ssh, a, resp

  Set ssh = CreateObject("WeOnlyDo.wodSSHCom.1")

  ssh.HostName = server
  ssh.Login = username
  ssh.Password = password

  ' set Blocking to true or false - blocking means the script is synchronous and waits for each method to complete
  ssh.Blocking = True

  ' Protocol: 0: Raw, 1: Telnet, 2: SSH1, 3: SSH2, 4: SSHAuto (Auto Negotiate)
  ssh.Protocol = 4

  ' remove ANSI codes from received data
  ssh.StripANSI = true

  'now connect
  ssh.Connect

  'wait for initial prompt
  ssh.WaitFor("regex:[\]\$] $")

  'issue a few commands, returning the output from the final one as the function result
  ssh.Execute "cd /usr/bin" & vbCRLF, "regex:[\]\$] $"
  SSHExample = ssh.Execute("ls -al" & vbCRLF, "regex:[\]\$] $")

  'finally disconnect
  ssh.Disconnect
End Function
VBEND

Input>server,Server:
Input>username,Username:
Input>password,Password:

// here we call the above VBS function to connect and run the command "ls -al"
VBEval>SSHExample("%server%","%username%","%password%"),response

//output the response
MessageModal>response

Notice the Execute statements above which execute a command and wait for the specified prompt to appear. The regex is simply waiting for any “]” or “$” found at the end of a line, which in my case signifies a prompt. Instead of using regex you can provide the exact prompt.

The component is nicely documented in the help file, with useful examples, so you should find it pretty easy to get up and running.

If you use any other components that work nicely with Macro Scheduler, let me know.