Skip to content

Instantly share code, notes, and snippets.

@sjcotto
Created November 2, 2013 01:19
Show Gist options
  • Save sjcotto/7274378 to your computer and use it in GitHub Desktop.
Save sjcotto/7274378 to your computer and use it in GitHub Desktop.
java exceute command line from ssh
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class Main {
private static String host = "##" + "";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession("##", host, 22);
session.setPassword("##");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(
channel.getInputStream()));
channel.setCommand("ls;");//separated by comma comands
channel.connect();
String msg = null;
while ((msg = in.readLine()) != null) {
System.out.println(msg);
}
channel.disconnect();
session.disconnect();
}
public static InputStream getStream(String str) throws Exception {
InputStream is = new ByteArrayInputStream(str.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
return is;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment