Skip to content

Instantly share code, notes, and snippets.

@kfei
Last active December 29, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfei/7716130 to your computer and use it in GitHub Desktop.
Save kfei/7716130 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.InputStream;
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 Exec {
public static void test(Session session) throws Exception {
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("pwd");
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
}
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("nck", "127.0.0.1", 22);
session.setPassword("asd");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
test(session); // This one succeeds with exit-status: 0
test(session); // This one fails with exit-status: 255
session.disconnect();
} catch (Exception e) {
//
}
}
}
/**
* Run several ssh commands in a single JSch session
*/
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;
class SshMultiCommands
{
public static void main(String[] args) throws Exception
{
JSch jsch = new JSch();
String user = "user"; //CHANGE ME
String host = "192.168.222.157"; //CHANGE ME
String passwd = "password"; //CHANGE ME
int port = 22;
Session session = jsch.getSession(user, host, port);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
InputStream input = channel.getInputStream();
//commands
ps.println("ls -lag");
ps.println("cd /etc");
ps.println("ls");
ps.println("exit");
ps.close();
printResult(input, channel);
channel.disconnect();
session.disconnect();
}
/**
* @param input
* @param channel
*/
private static void printResult(InputStream input,
Channel channel) throws Exception
{
int SIZE = 1024;
byte[] tmp = new byte[SIZE];
while (true)
{
while (input.available() > 0)
{
int i = input.read(tmp, 0, SIZE);
if(i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed())
{
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try
{
Thread.sleep(300);
}
catch (Exception ee)
{
}
}
}
}
package examples.com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 TempFile {
private static Session connectSession(String userName, String password, String host) throws Exception {
JSch.setLogger(new MyLogger());
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(userName, host, 22);
session.setPassword(password);
session.setTimeout(10000);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
}
catch (JSchException ex) {
System.out.println(ex);
}
return session;
}
private static void connectChannel(Session session, String command) throws JSchException, IOException {
System.out.println(command);
Channel channel = null;
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
System.out.println("channel.isConnected() "+channel.isConnected());
setInAndOutStream(channel, in);
channel.disconnect();
System.out.println("channel.isClosed() "+channel.isClosed());
}
private static void setInAndOutStream(Channel channel, InputStream in) throws IOException, JSchException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder outPutResult = new StringBuilder("");
int exitStatus = -100;
String output;
while (true) {
while (true) {
try {
String result = br.readLine();
if (result == null)
break;
outPutResult.append(result);
} catch (Exception ex) {
ex.printStackTrace();
break;
}
}
output = outPutResult.toString();
if (channel.isClosed()) {
exitStatus = channel.getExitStatus();
break;
}
}
System.out.println(exitStatus);
System.out.println(output);
}
private static void executeCommand(Session session, String command) throws JSchException, IOException{
connectChannel(session,command);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("starting");
// TODO Auto-generated method stub
try {
Session session = connectSession("username", "password", "host");
if(session!=null) {
executeCommand(session, "ls");
executeCommand(session, "cp out.txt copy.txt");
session.disconnect();
}
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done..");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment