当前位置:网站首页>Using SSH

Using SSH

2022-07-04 19:00:00 User 7741497

%Net.SSH Package support SSH( Safety enclosure ) signal communication . This topic briefly introduces the classes in this package .

establish SSH conversation

%Net.SSH.Session Express SSH conversation . To use this class , Do the following :

  1. Create an instance of a class .
  2. Use Connect() The instance method connects to the server .
  3. Use AuthenticateWithKeyPair() or AuthenticateWithUsername() Authenticate to the server .
  4. Use %Net.SSH.Session Other methods of executing a single file in and out of a remote system SCP( Secure replication ) operation 、 Execute remote command 、 transmission TCP Communication or execution SFTP operation .

for example , Use SFTP Use the session for SFTP operation . This method returns by reference that can be used for SFTP Operation of the %Net.SSH.SFTP example .

Important note : Information about supported platforms that can use these classes , see also %Net.SSH.Session and %Net.SSH.SFTP Class reference for .

Example : adopt SFTP Lists the files

The following method shows how to pass SFTP Write the file list on the server :

Method SFTPDir(ftpserver, username, password) As %Status
{
    set ssh = ##class(%Net.SSH.Session).%New()
    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
}

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()
    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
}
/// 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()
    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 "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["<READ>":$$$OK,1:$$$ERROR($$$CacheError,$ZE))
    Goto Exit
}
原网站

版权声明
本文为[User 7741497]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041708542147.html