Chapter 13 Using SSH

% net.ssh Software package supports SSH(security shell) communication. This topic provides a brief introduction to the classes in this package.

Creating an SSH Session

% net.ssh. Session Indicates an SSH Session. To use this class do the following:

  1. Create an instance of the class.
  2. useConnect()The instance method connects to the server.
  3. useAuthenticateWithKeyPair()orAuthenticateWithUsername()Authenticate to the server.
  4. use%Net.SSH.SessionOther methods perform SCP(secure replication) operations on individual files in and out of a remote system, execute remote commands, transmit TCP traffic, or perform SFTP operations.

For example, using SFTP to use a session for SFTP operations. This method returns an instance of % net.ssh.sftp available for SFTP operations by reference.

Important: See the class references for % net.ssh. Session and % net.ssh.sftp for information about the supported platforms on which these classes can be used.

Example: List files using SFTP

The following method shows how to write a list of files to a server using SFTP:

Method SFTPDir(ftpserver, username, password) As %Status
{
    set ssh = ##class(%Net.SSH.Session%).New(a)do ssh.Connect(ftpserver)
    do ssh.AuthenticateWithUsername(username.password) / /open an SFTP session and get that returned by reference
    do ssh.OpenSFTP(.sftp) / /get a list of files
    do sftp.Dir(".",.files)
    set i=$ORDER(files(""))
    while i'="" { write $listget(files(i),1),! set i=$ORDER(files(i)) } quit $$$OK }Copy the code

Other examples

/// Demonstrates the execution of a remote command (by default, uname -a).
ClassMethod TestExecute(host As %String, username As %String, password As %String, command As %String = "uname -a", pTimeout As %Integer = -1) As %Status
{
	Set s = ##class(%Net.SSH.Session%).New(a)Set sc = s.Connect(host)
	Quit:$$$ISERR(sc) sc
	If pTimeout'=-1 { Set sc = s.SetTimeout(pTimeout) Quit:$$$ISERR(sc) sc } Set sc = s.AuthenticateWithUsername(username,password) Quit:$$$ISERR(sc) sc Set sc = s.Execute(command,.tDevice) Quit:$$$ISERR(sc) sc Set $ZT="Trap" For { Use tDevice Read X Use $P If X'[$C(13) {
			For i=1:1:$L(X,$C(10)) Write $P(X,$C(10),i),!
		} Else {
			Write X
		}
	}
Exit
	Use $P
	Close tDevice
	Quit sc
Trap
	Set sc = $S($ZE["<READ>":$$$OK,1:$$$ERROR($$$CacheError,$ZE))
	Goto Exit
}
Copy the code
/// Demonstrates the use of port forwarding to whatismyipaddress.com via the remote SSH server.
ClassMethod TestForwardPort(host As %String, username As %String, password As %String, remotehost As %String = "whatismyipaddress.com", remoteport As %Integer = 80) As %Status
{
	Set s = ##class(%Net.SSH.Session%).New(a)Set sc = s.Connect(host)
	Quit:$$$ISERR(sc) sc
	Set sc = s.AuthenticateWithUsername(username,password)
	Quit:$$$ISERR(sc) sc
	Set sc = s.ForwardPort(remotehost,remoteport,.tDevice)
	Quit:$$$ISERR(sc) sc
	
	Set $ZT="Trap"
	
	Use tDevice
	Write "The GET/HTTP / 1.0"_$C(13.10.13.10)
	Write *-3 // Flush
	
	// Now the response
	For {
		Use tDevice
		Read X
		Use $P
		If X'[$C(13) { For i=1:1:$L(X,$C(10)) Write $P(X,$C(10),i),!  } Else { Write X } } Exit Use $P Close tDevice Quit sc Trap Set sc = $S($ZE["
      
       ":$$$OK,1:$$$ERROR($$$CacheError,$ZE)) Goto Exit }
      Copy the code