Created
November 2, 2013 01:19
-
-
Save sjcotto/7274378 to your computer and use it in GitHub Desktop.
java exceute command line from ssh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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