This code is an example to use SSH on a demo (readonly) server. 
Since this server returns a Carriage Return and LineFeed after each character (don’t ask why…), I used the MultiReplace function to adjust the outcome. All double CRLF (real CRLF) are replaced by LF for later use. All single CRLF are replaced by “”. Then the previous stored LF are replaced back to CRLF. The other replacements are to make sure system characters and color codes are not returned.


' Note that a connection remains open until disconnected.

dim result, connectResult


'It seems that for this testserver, a CR and LF is provided after 

'each character. Therefore we would like to remove those extra 

'linefeeds and additional ascii codes

SSHMULTIREPLACEXML = [<Replacements>


  <!-- REPLACE DOUBLE LINEFEEDS TO LF FOR LATER USE -->

  <Replacement>

    <Search>#CRLF##CRLF#</Search>

    <ReplaceBy>#LF#</ReplaceBy>

  </Replacement>


  <!-- REPLACE CRLF TO "", we do not need those -->

  <Replacement>

    <Search>#CRLF#</Search>

    <ReplaceBy></ReplaceBy>

  </Replacement>


  <!-- REPLACE THE PREVIOUS SAVED LF BACK TO CRLF -->

….

  <!-- REPLACE SYSTEM ASCII CHARACTER ESCAPE TO "" -->

  <Replacement>

    <SearchChar>0</SearchChar>

    <ReplaceBy />

  </Replacement>

</Replacements>]


'Connect via SFTP to a test website

connectResult = SSHConnect("test.rebex.net","demo","password")


'Check if a connection exists

Output = [SSH Connection exists: #SSHCONNECTED##CRLF#]


'If not connected skip the next steps

if not (SSHConnected) then

       goto end

endif


'Open a seperate shell and run all commands to obtain a directory listing

'After the RunCommand, the shell is closed again so directory changes are also 'lost'.

result = SSHRunCommand("cd /pub/example && ls -a")

Add2Output = [Runcommand result: #CRLF##RESULT##CRLF##CRLF#]


'Now return all output from command as seen by SSH Clients

Add2Output = [#CONNECTRESULT#]


Add2Output = SSHSendCommand("cd /pub/example")

Add2Output = SSHSendCommand("ls -a")


'Now close the connection!

SSHDisconnect()


The RunCommand function opens a separate shell and runs the command (s) and then closes the shell again . Very easy to use for fixed scripts.


The SendCommand function handles a single command, but keeps the shell open. Multiple commands can be send to the shell and every returned value can be analysed and handled.


Note that a connection remains open until disconnected by code or by closing IT-Tool.


Created with the Personal Edition of HelpNDoc: News and information about help authoring tools and software