首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JAVA-SSH2:JSch试工

2012-08-25 
JAVA-SSH2:JSch试用试用开源软件JSch实现通过SSH2协议,在JAVA程序内远程执行linux命令。?官方网站:http://w

JAVA-SSH2:JSch试用

试用开源软件JSch实现通过SSH2协议,在JAVA程序内远程执行linux命令。

?

官方网站:http://www.jcraft.com/

?

?

package ssh;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.nio.charset.Charset;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelExec;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;public class JSchDemo{        public static void main(String[] args) throws Exception    {        String charset = "UTF-8";        String user = "root";        String passwd = "123456";        String host = "40.40.40.50";        String command = "ls -l";                JSch jsch = new JSch();        Session session = jsch.getSession(user, host, 22);        session.setPassword(passwd);        java.util.Properties config = new java.util.Properties();        config.put("StrictHostKeyChecking", "no");        session.setConfig(config);        session.connect();                Channel channel = session.openChannel("exec");        ((ChannelExec) channel).setCommand(command);        channel.setInputStream(null);        ((ChannelExec) channel).setErrStream(System.err);                channel.connect();        InputStream in = channel.getInputStream();        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));        String buf = null;        while ((buf = reader.readLine()) != null)        {            System.out.println(buf);        }        reader.close();        channel.disconnect();        session.disconnect();    }    }

热点排行