Now-a-days, we can see that whole world is moving around Clouds and virtualization. More and more applications are building for managing datacentre servers and other stuff. I have been part of one of such a module. I developed one module for monitoring and managing Linux servers remotely. We used JCraft’s Jsch with Google’s Expect4j for the same. Let’s get some idea about those API in brief.

JSCH

As the website suggests, it is pure implementation of SSH2. It is very easy to use and integrate into your program. It is a fact that it is not well documented. You can read more about JSchfrom its website.

Expect4j

Expect is the kitchen sink of IO control. It supports control of processes and sockets, and a complex method of match multiple patterns at the same time. This is what Google code has to say about Expect4j. When you executes commands on remote machines one after other, your program needs to know when to execute next command. It’s like send your command and wait for execution of the same. Or we can say that wait for the command prompt. Expect4j does similar stuff(as far as I know). It also provides closures for getting complete output log of executed commands. I don’t know if there is some other use of closures.

Now, let’s start with an example. First open up SSH connection on remote machine.

1
2
3
4
5
6
7
8
9
10
11
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
 
Hashtable<String,String> config = new Hashtable<String,String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(60000);
ChannelShell channel = (ChannelShell) session.openChannel("shell");
Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
channel.connect();

You can see that we have opened “shell” channel. That is because we want to execute sequence of commands on linux shell. You can see that Expect4j is initialized from JSCH session.

Now, we will prepare RegX patterns of command prompts of targeted machine. Be careful here because if these are not right then you might end up not executing commands. You can also provide closure provided by Expect4j to get output of your commands. (There might be some other use of it.)

1
2
3
4
5
6
StringBuilder buffer = new StringBuilder();
Closure closure = new Closure() {
            public void run(ExpectState expectState) throws Exception {
                buffer.append(expectState.getBuffer());//string buffer for appending output of executed command
            }
};
1
2
3
4
5
6
7
8
9
10
11
12
String[] linuxPromptRegEx = new String[]{"\\>","#"};
List<Match> lstPattern =  new ArrayList<Match>();
        for (String regexElement : linuxPromptRegEx) {
            try {
                Match mat = new RegExpMatch(regexElement, closure);
                lstPattern.add(mat);
            } catch (MalformedPatternException e) {
                e.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            }
}

You can see that we have defined a closure that appends output of every command executed. We are providing this closure to every command pattern. I have my linux shell prompt working at “/>” and “#”. You can define your prompts as per your linux box.

Now, start executing commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
List<String> lstCmds = new ArrayList<String>();
lstCmds.add("ls");
lstCmds.add("pwd");
lstCmds.add("mkdir testdir");
 
for(String strCmd : lstCmds) {
    int returnVal = expect.expect(objPattern);
    if (returnVal == -2) {
        expect.send(strCommandPattern);
        expect.send("\r");//enter character
    }
}

We have three commands to execute in a single continuous SSH session.  First it expects one of its prompt patterns to match. If right prompt has encountered then send one command over the shell to execute.  Immediately after the command, send an enter character to execute the command. After that again wait for one of your command prompt to occur and then send other command. So, we can see that it send/wait kind of mechanism.

Now, put all together and here is a SSH client that can execute sequence of command on remote linux box. You will need following libraries in your project to execute this test class.

  • expect4j-1.0.jar
  • jakarta-oro.jar
  • jsch-0.1.44.jar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import org.apache.oro.text.regex.MalformedPatternException;
 
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
 
import expect4j.Closure;
import expect4j.Expect4j;
import expect4j.ExpectState;
import expect4j.matches.Match;
import expect4j.matches.RegExpMatch;
 
public class SSHClient {
 
    private static final int COMMAND_EXECUTION_SUCCESS_OPCODE = -2;
    private static String ENTER_CHARACTER = "\r";
    private static final int SSH_PORT = 22;
    private List<String> lstCmds = new ArrayList<String>();
    private static String[] linuxPromptRegEx = new String[]{"\\>","#", "~#"};
 
    private Expect4j expect = null;
    private StringBuilder buffer = new StringBuilder();
    private String userName;
    private String password;
    private String host;
 
    /**
     *
     * @param host
     * @param userName
     * @param password
     */
    public SSHClient(String host, String userName, String password) {
        this.host = host;
        this.userName = userName;
        this.password = password;
    }
    /**
     *
     * @param cmdsToExecute
     */
    public String execute(List<String> cmdsToExecute) {
        this.lstCmds = cmdsToExecute;
 
        Closure closure = new Closure() {
            public void run(ExpectState expectState) throws Exception {
                buffer.append(expectState.getBuffer());
            }
        };
        List<Match> lstPattern =  new ArrayList<Match>();
        for (String regexElement : linuxPromptRegEx) {
            try {
                Match mat = new RegExpMatch(regexElement, closure);
                lstPattern.add(mat);
            } catch (MalformedPatternException e) {
                e.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
 
        try {
            expect = SSH();
            boolean isSuccess = true;
            for(String strCmd : lstCmds) {
                isSuccess = isSuccess(lstPattern,strCmd);
                if (!isSuccess) {
                    isSuccess = isSuccess(lstPattern,strCmd);
                }
            }
 
            checkResult(expect.expect(lstPattern));
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeConnection();
        }
        return buffer.toString();
    }
    /**
     *
     * @param objPattern
     * @param strCommandPattern
     * @return
     */
    private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
        try {
            boolean isFailed = checkResult(expect.expect(objPattern));
 
            if (!isFailed) {
                expect.send(strCommandPattern);
                expect.send(ENTER_CHARACTER);
                return true;
            }
            return false;
        } catch (MalformedPatternException ex) {
            ex.printStackTrace();
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
    /**
     *
     * @param hostname
     * @param username
     * @param password
     * @param port
     * @return
     * @throws Exception
     */
    private Expect4j SSH() throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(userName, host, SSH_PORT);
        if (password != null) {
            session.setPassword(password);
        }
        Hashtable<String,String> config = new Hashtable<String,String>();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect(60000);
        ChannelShell channel = (ChannelShell) session.openChannel("shell");
        Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
        channel.connect();
        return expect;
    }
    /**
     *
     * @param intRetVal
     * @return
     */
    private boolean checkResult(int intRetVal) {
        if (intRetVal == COMMAND_EXECUTION_SUCCESS_OPCODE) {
            return true;
        }
        return false;
    }
    /**
     *
     */
    private void closeConnection() {
        if (expect!=null) {
            expect.close();
        }
    }
    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        SSHClient ssh = new SSHClient("linux_host", "root", "password");
        List<String> cmdsToExecute = new ArrayList<String>();
        cmdsToExecute.add("ls");
        cmdsToExecute.add("pwd");
        cmdsToExecute.add("mkdir testdir");
        String outputLog = ssh.execute(cmdsToExecute);
        System.out.println(outputLog);
    }
}

If you find any difficulties to execute it, just play around execute command loop and command prompts RegX patterns.

Remote SSH: Using JSCH with Expect4j的更多相关文章

  1. The remote SSH server rejected X11 forwarding request

    两台相同的虚拟机,一台没有错误,一个经常出现警告,内容如下所示: The remote SSH server rejected X11 forwarding request 找了很多方法,最后发现是安 ...

  2. Xshell报错“The remote SSH server rejected X11 forwarding request.”

    Xshell报错“The remote SSH server rejected X11 forwarding request.” 2012年12月17日 ⁄ Linux⁄ 共 218字 ⁄ 字号 小  ...

  3. Xshell 连接虚拟机出现 "The remote SSH server rejected X11 forwarding request"

    1. 描述 虚拟机:VirtualBox Linux: centOS7 解决了 centOS7在VirtualBox中装好后的网络连接问题 后,用 Xshell 连接服务器时出现下面情况: 2. ss ...

  4. 解决 Xshell 连接出现 The remote SSH server rejected X11 forwarding request 问题

    问题描述 使用 Xshell 5 首次连接虚拟机 CentOS 7.6 出现这样的提示: WARNING! The remote SSH server rejected X11 forwarding ...

  5. 解决"The remote SSH server rejected X11 forwarding request"问题

    今天突然想起来好久没有登录我的vps了,于是下载了xshell,填入地址登录后,看到提示"WARNING! The remote SSH server rejected X11 forwar ...

  6. 解决“WARNINGThe remote SSH server rejected X11 forwarding request.“警告

    使用xshell连接服务器时,出现了"WARNING! The remote SSH server rejected X11 forwarding request.",意思是&qu ...

  7. SELINUX设为Disable 影响java SSH工具包Jsch 0.1.49.jar的一个案例

    最近项目中遇到一个典型事件,当RHEL 的SELINUX设为DISABLE时 使用JAVA的Jsch 库调用SSH命令时将随机返回空字符串,我使用的版本是0.1.49,最新版本0.1.51未测试. 关 ...

  8. java控制远程ssh-JSCH(二)

    github: https://github.com/wengyingjian/ssh-java-demo.git 这次找到了一套新的api,叫jsch.网上查了一下,顺便把官网的几个demo给一通拿 ...

  9. VS Code Remote SSH设置

    本文翻译自:5 Steps: Setup VS Code for Remote Development via SSH from Windows to Linux system 5个步骤:设置VS代码 ...

随机推荐

  1. Mac上把python源文件编译成so文件

    把python源文件编译成so文件 前言 实际上属于一种代码混淆/加密的技术,大家知道python的源文件放在那里,大家是都可以看的,不像C语言编译出来可以拿编译后的东西去运行,所以就出现了这种需求. ...

  2. JIRA python篇之展示多人未完成任务列表

    [本文出自天外归云的博客园] 通过python中的jira类我们可以方便的操作jira,获取一些我们想要再加工的信息. 这里举例,用html页面的形式展示分派给组内每个人的任务(未完成的.正在进行中的 ...

  3. 这到底是什么bug?---已结贴

    问题描述:全局变量,会被莫名其妙更改!打印为50,后面做比较的时候这个值为0了. 第一,我肯定没有犯低级错误,没有其他的更改,搜索全部代码,没有发现这个变量因为我程序问题导致不符合预期,同时找了两位同 ...

  4. django HttpRequest对象

    概述: 服务器接收http请求后,会根据报文创建HttpRequest对象 视图的第一个参数就是HttpRequest对象 django创建的,之后调用视图时传递给视图 属性 path:请求的完整路径 ...

  5. [转]为Kindeditor控件添加图片自动上传功能

    原文地址:http://www.cnblogs.com/jaxu/p/3824583.html Kindeditor是一款功能强大的开源在线HTML编辑器,支持所见即所得的编辑效果.它使用JavaSc ...

  6. [转]oracle制定定时任务(dbms_jobs)

    原文地址:http://www.cnblogs.com/siashan/p/4183868.html 本节摘要:本节介绍使用oracle自带的job来实现oracle定制定时执行任务.   1.引言 ...

  7. 编译 Linux 3.5 内核烧写 Android 4.2.2 到 Tiny4412 开发板

    . . . . . 昨天已经编译了 Android 4.2.2 的源码,详见<Ubuntu 14.04 编译 Android 4.2.2 for Tiny4412>一文. 今天我们继续剩下 ...

  8. Go Revel - Results(响应)

    每个`Action`必须返回一个`revel.Result`实例,用来处理响应.它遵循了简单的接口: type Result interface { Apply(req *Request, resp ...

  9. [转]Java中的JavaBean类

    一. javabean 是什么? Bean的中文含义是"豆子",顾名思义,JavaBean是指一段特殊的Java类, 就是有默然构造方法,只有get,set的方法的java类的对象 ...

  10. Javascript全栈技术架构

    https://worktile.com/tech/basic/the-worktile-tech-stack https://worktile.com/tech/basic/worktile-rea ...