'Example executing ls command on remote host using jSch
'Ensure #AdditionalJar: jsch-0.1.51
Sub ExecuteRemoteCommand
Dim hostcmd As String = "ls"
Dim user As String = "<USERNAME>"
Dim host As String = "<HOST ADDRESS>"
Dim port As Int = 22
Dim pw As String = "<PASSWORD>"
Dim joSch As JavaObject
'Java: JSch js = new JSch();
joSch.InitializeNewInstance("com.jcraft.jsch.JSch", Null)
'Java: Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
Dim joS As JavaObject
joS = joSch.RunMethod("getSession", Array(user, host, port))
'Java: s.setPassword("mypassword");
joS.RunMethod("setPassword", Array(pw))
'Java: Properties config = new Properties();
Dim joP As JavaObject
joP = joP.InitializeNewInstance("java.util.Properties", Null)
'Java: config.put("StrictHostKeyChecking", "no");
joP.RunMethod("setProperty", Array("StrictHostKeyChecking", "no"))
'Java: s.setConfig(config);
joS.RunMethod("setConfig", Array(joP))
'Java: s.connect();
joS.RunMethod("connect", Null)
Log("Connected to Host...")
Log("Invoking Command..." & hostcmd )
'Java: Channel c = s.openChannel("exec");
Dim joC As JavaObject = joS.RunMethod("openChannel", Array("exec"))
joC.RunMethod("setCommand", Array(hostcmd ))
'Java: joC.RunMethod("setCommand", Array("ls -l"))
Log("Invoking Command...Connect")
joC.RunMethod("connect", Null)
Log("InputstreamReader...Build")
Dim joISR As JavaObject
'Java: BufferedReader reader = new BufferedReader(new InputStreamReader(cE.getInputStream()));
joISR.InitializeNewInstance("java.io.InputStreamReader", Array(joC.RunMethod("getInputStream", Null)))
Log("BufferedReader...Reading")
Dim joBR As JavaObject
joBR.InitializeNewInstance("java.io.BufferedReader", Array(joISR))
'Java: joBR.RunMethod("readLine", Null)
Log("Result Command " & hostcmd )
Dim s As String
Do While s.Contains("null") = False
s = joBR.RunMethod("readLine", Null)
If s <> "null" Then Log(s)
Loop
End Sub
Hi Shay,
either you wrap the jSch Library or you use JavaObject to access.
Example lexecuting command 'ls' tested on Raspberry Pi and TinkerForge RED Brick Host.
YES, its is bit of JavaObject juggle, but works fine - soon InLine Java will come, which makes these kind of code easier
B4X:'Example executing ls command on remote host using jSch 'Ensure #AdditionalJar: jsch-0.1.51 Sub ExecuteRemoteCommand Dim hostcmd As String = "ls" Dim user As String = "<USERNAME>" Dim host As String = "<HOST ADDRESS>" Dim port As Int = 22 Dim pw As String = "<PASSWORD>" Dim joSch As JavaObject 'Java: JSch js = new JSch(); joSch.InitializeNewInstance("com.jcraft.jsch.JSch", Null) 'Java: Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22); Dim joS As JavaObject joS = joSch.RunMethod("getSession", Array(user, host, port)) 'Java: s.setPassword("mypassword"); joS.RunMethod("setPassword", Array(pw)) 'Java: Properties config = new Properties(); Dim joP As JavaObject joP = joP.InitializeNewInstance("java.util.Properties", Null) 'Java: config.put("StrictHostKeyChecking", "no"); joP.RunMethod("setProperty", Array("StrictHostKeyChecking", "no")) 'Java: s.setConfig(config); joS.RunMethod("setConfig", Array(joP)) 'Java: s.connect(); joS.RunMethod("connect", Null) Log("Connected to Host...") Log("Invoking Command..." & hostcmd ) 'Java: Channel c = s.openChannel("exec"); Dim joC As JavaObject = joS.RunMethod("openChannel", Array("exec")) joC.RunMethod("setCommand", Array(hostcmd )) 'Java: joC.RunMethod("setCommand", Array("ls -l")) Log("Invoking Command...Connect") joC.RunMethod("connect", Null) Log("InputstreamReader...Build") Dim joISR As JavaObject 'Java: BufferedReader reader = new BufferedReader(new InputStreamReader(cE.getInputStream())); joISR.InitializeNewInstance("java.io.InputStreamReader", Array(joC.RunMethod("getInputStream", Null))) Log("BufferedReader...Reading") Dim joBR As JavaObject joBR.InitializeNewInstance("java.io.BufferedReader", Array(joISR)) 'Java: joBR.RunMethod("readLine", Null) Log("Result Command " & hostcmd ) Dim s As String Do While s.Contains("null") = False s = joBR.RunMethod("readLine", Null) If s <> "null" Then Log(s) Loop End Sub
Sub Process_Globals
Public NativeMe As JavaObject
...
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
NativeMe = Me
Dim l As List = NativeMe.RunMethod("jschexeccmd", Array("username", "hostaddress", "password", "hostcommand"))
Log("Result (Lines:" & l.Size & ")")
For i = 0 To l.Size - 1
Log(l.Get(i))
Next
End Sub
#If JAVA
import java.util.*;
import java.io.*;
import com.jcraft.jsch.*;
public static List<Object> jschexeccmd(String user, String host, String pw, String cmd) {
List<Object> listJava = new ArrayList<Object>();
try{
JSch js = new JSch();
Session s = js.getSession(user, host, 22);
s.setPassword(pw);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand(cmd);
ce.setErrStream(System.err);
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
listJava.add(line);
}
ce.disconnect();
s.disconnect();
}
catch(Exception e){
System.out.println(e);
}
return listJava;
}
#End If
Hi,
as a followup using InLine Java, find the previous example converted using B4J 2.80. Tested again under Raspian and Debian.
Pretty simple to use and a really cool feature of B4A / B4J.
Note that the returned Java List is straight used as a B4J List.
B4X:Sub Process_Globals Public NativeMe As JavaObject ... Sub AppStart (Form1 As Form, Args() As String) MainForm = Form1 NativeMe = Me Dim l As List = NativeMe.RunMethod("jschexeccmd", Array("username", "hostaddress", "password", "hostcommand")) Log("Result (Lines:" & l.Size & ")") For i = 0 To l.Size - 1 Log(l.Get(i)) Next End Sub #If JAVA import java.util.*; import java.io.*; import com.jcraft.jsch.*; public static List<Object> jschexeccmd(String user, String host, String pw, String cmd) { List<Object> listJava = new ArrayList<Object>(); try{ JSch js = new JSch(); Session s = js.getSession(user, host, 22); s.setPassword(pw); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); s.setConfig(config); s.connect(); Channel c = s.openChannel("exec"); ChannelExec ce = (ChannelExec) c; ce.setCommand(cmd); ce.setErrStream(System.err); ce.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream())); String line; while ((line = reader.readLine()) != null) { listJava.add(line); } ce.disconnect(); s.disconnect(); } catch(Exception e){ System.out.println(e); } return listJava; } #End If