iT邦幫忙

0

【有人可以幫忙測試一下 JAVA Expect 嗎???】

  • 分享至 

  • xImage

我有用一個套件
ExpectIt
https://github.com/Alexey1Gavrilov/ExpectIt
我主要是要用在維護網路設備
因為家裡沒有設備可以測試
所以我在家用虛擬電腦灌 ubuntu
再從實機(windows)連進去測試
我的程式碼如下

import com.jcraft.jsch.*;
import net.sf.expectit.*;
import net.sf.expectit.matcher.Matcher;
import java.util.Properties;
import static net.sf.expectit.matcher.Matchers.matches;
import static net.sf.expectit.matcher.Matchers.regexp;
import java.io.IOException;
import java.io.FileWriter;
import java.lang.String;

public class jsch_expect {
    public static void main(String[] args) {
        try {
            JSch jSch = new JSch();
            Session session = jSch.getSession("andyto202", "192.168.213.128");
            session.setPassword("to");
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            Channel channel = session.openChannel("shell");
            channel.connect();
            Expect expect = new ExpectBuilder()
                    .withOutput(channel.getOutputStream())
                    .withInputs(channel.getInputStream(), channel.getExtInputStream())
                    .withEchoOutput(System.out)
                    .withEchoInput(System.err)
                    //        .withInputFilters(removeColors(), removeNonPrintable())
                    .withExceptionOnFailure()
                    .build();
            expect.expect(regexp("~"));
            expect.sendLine("ifconfig");
            expect.expect(regexp("~"));
            expect.sendLine("exit");
            expect.close();
            channel.disconnect();
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

執行結果如下


andyto202@ubuntu:~$ ifconfig
ifconfig
ens33 Link encap:Ethernet HWaddr 00:0c:29:f4:27:0c
inet addr:192.168.213.128 Bcast:192.168.213.255 Mask:255.255.255.0
inet6 addr: fe80::1ec9:ab94:33d:6c55/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4878 errors:0 dropped:0 overruns:0 frame:0
TX packets:2000 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6713349 (6.7 MB) TX bytes:127984 (127.9 KB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:299 errors:0 dropped:0 overruns:0 frame:0
TX packets:299 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:23390 (23.3 KB) TX bytes:23390 (23.3 KB)

andyto202@ubuntu:~$ exit

但是我不想要列出所有的結果
如果我只要列出

192.168.213.128
192.168.213.255
255.255.255.0

請問該怎麼做呢??
謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
fillano
iT邦超人 1 級 ‧ 2016-07-03 21:18:11

你都傳regexp()進去了,把他改一改去抓你要的pattern就好了吧?

andyto202 iT邦研究生 4 級 ‧ 2016-07-04 00:31:27 檢舉

謝謝 fillano

我有寫一段

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
    public static void main(String[] args) {

        String a = "inet addr:192.168.213.128  Bcast:192.168.213.255  Mask:255.255.255.0";
        Pattern pattern = Pattern.compile("\\d+.\\d+.\\d+.\\d+");
        Matcher matcher = pattern.matcher(a);
        System.out.println(a.matches("\\d+"));
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

但是我不知道要怎麼把這個套到我問的那個裡面

_<

andyto202 iT邦研究生 4 級 ‧ 2016-07-04 00:34:03 檢舉

應該是這樣子才對

 */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
    public static void main(String[] args) {

        String a = "inet addr:192.168.213.128  Bcast:192.168.213.255  Mask:255.255.255.0";
        Pattern pattern = Pattern.compile("\\d+.\\d+.\\d+.\\d+");
        Matcher matcher = pattern.matcher(a);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

System.out.println(a.matches("\d+"));
是測試語法用的
多餘的

fillano iT邦超人 1 級 ‧ 2016-07-04 11:53:54 檢舉

改成這樣

        Pattern pattern = Pattern.compile("inet addr:(\\d+.\\d+.\\d+.\\d+)");
        Matcher matcher = pattern.matcher(a);
        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }

看看。

我要發表回答

立即登入回答