欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

采用JSCH实现对linux远程操作,执行简单的命令,jschlinux,import java.

来源: javaer 分享于  点击 41554 次 点评:286

采用JSCH实现对linux远程操作,执行简单的命令,jschlinux,import java.


import java.io.BufferedReader;import java.io.IOException;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.JSchException;import com.jcraft.jsch.Session;public class JSchDemo {    private String charset = "UTF-8"; // 设置编码格式    private String user; // 用户名    private String passwd; // 登录密码    private String host; // 主机IP    private JSch jsch;    private Session session;    /**     *      * @param user用户名     * @param passwd密码     * @param host主机IP     */    public JSchDemo(String user, String passwd, String host) {        this.user = user;        this.passwd = passwd;        this.host = host;    }    /**     * 连接到指定的IP     *      * @throws JSchException     */    public void connect() throws JSchException {        jsch = new JSch();        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();    }    /**     * 执行相关的命令     */    public void execCmd() {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String command = "";        BufferedReader reader = null;        Channel channel = null;        try {            while ((command = br.readLine()) != null) {                channel = session.openChannel("exec");                ((ChannelExec) channel).setCommand(command);                channel.setInputStream(null);                ((ChannelExec) channel).setErrStream(System.err);                channel.connect();                InputStream in = channel.getInputStream();                reader = new BufferedReader(new InputStreamReader(in,                        Charset.forName(charset)));                String buf = null;                while ((buf = reader.readLine()) != null) {                    System.out.println(buf);                }            }        } catch (IOException e) {            e.printStackTrace();        } catch (JSchException e) {            e.printStackTrace();        } finally {            try {                reader.close();            } catch (IOException e) {                e.printStackTrace();            }            channel.disconnect();            session.disconnect();        }    }    public static void main(String[] args) throws Exception {        String user = "root";        String passwd = "123456";        String host = "192.168.1.188";        JSchDemo demo = new JSchDemo(user, passwd, host);        demo.connect();        demo.execCmd();    }}//该片段来自于http://byrx.net
相关栏目:

用户点评