/*
* NetRexx SSH example:
*
* You will need to install the Jar file from:
*
*
* or from your software/package manager
*
* Arg0 = filename of known_hosts file
* Arg1 = username of account on remote system
* Arg2 = hostname or IP of remote system
* Arg3 = port to use
* Arg4 = password for above username
* Arg5 = connectTimeout
* Arg6 = command to send to remote system
*
* Example Usage after NetRexx Compile:
*
* java Sample '/home/user/.ssh/known_hosts' 'user' '127.0.0.1' '22' 'passwd' '60000' 'ls'
*
*/
import com.jcraft.jsch.
class Sample
method main(argwords=String[]) static
Instance_of_JSch = JSch()
filename = String argwords[0]
username = String argwords[1]
host = String argwords[2]
port = int argwords[3]
password = String argwords[4]
connectTimeout = int argwords[5]
Instance_of_Session = Session
command = String argwords[6]
/* WARNING this is UNSECURE. UNCOMMENT only for TROUBLE SHOOTING */
--Instance_of_JSch.setConfig("StrictHostKeyChecking", "no")
do
Instance_of_JSch.setKnownHosts(filename)
catch ex=JSchException
say ex.getMessage()
end
do
Instance_of_Session = Instance_of_JSch.getSession(username, host, port)
Instance_of_Session.setPassword(password)
Instance_of_Session.connect(connectTimeout)
catch ex=JSchException
say ex.getMessage()
end
sb = StringBuilder()
do
channel = Instance_of_Session.openChannel("exec")
(ChannelExec channel).setCommand(command)
stream = channel.getInputStream()
channel.connect()
data = stream.read()
loop while data \== -1
sb.append(Rexx(data).d2c)
data = stream.read()
end
channel.disconnect()
catch e=IOException
say e.getMessage()
catch ex=JSchException
say ex.getMessage()
end
say sb.toString()
Instance_of_Session.disconnect()