Add molude: nemo-tools and remove the depency of cliche repositoty.
[nemo.git] / nemo-tools / sandbox / src / main / java / org / opendaylight / nemo / tool / sandbox / CmdExecutor.java
1 /*
2  * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.nemo.tool.sandbox;
10
11 import ch.ethz.ssh2.Connection;
12 import ch.ethz.ssh2.Session;
13 import ch.ethz.ssh2.StreamGobbler;
14 import org.opendaylight.nemo.tool.sandbox.utils.Config;
15
16 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Created by hj on 12/9/15.
25  */
26 public class CmdExecutor {
27     private static Logger log = LoggerFactory.getLogger(CmdExecutor.class);
28     private static Connection sshConnection;
29     private static String strHostName = Config.getHostName();
30     private static String strUserName = Config.getHostUser();
31     private static String strPassword = Config.getHostPassword();
32
33     public static boolean open() {
34         try {
35             sshConnection = new Connection(strHostName);
36             sshConnection.connect();
37             if (false == sshConnection.authenticateWithPassword(strUserName, strPassword)) {
38                 throw new IOException("Authentication failed!");
39             } else {
40                 return true;
41             }
42         } catch (IOException objException) {
43             log.error("Exceptions:",objException);
44             if (null != sshConnection) {
45                 sshConnection.close();
46                 sshConnection = null;
47             }
48         }
49         return false;
50     }
51
52     public static void close() {
53         if (null != sshConnection) {
54             sshConnection.close();
55             sshConnection = null;
56         }
57     }
58
59     public static String sshExecute(String command) {
60         Session session = null;
61         try {
62             session = sshConnection.openSession();
63             if (!"root".equals(Config.getHostName())) {
64                 command = "sudo " + command;
65             }
66             session.execCommand(command);
67             InputStream stdout = new StreamGobbler(session.getStdout());
68             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stdout));
69             String result = "";
70             while (true) {
71                 String line = bufferedReader.readLine();
72                 if (line != null)
73                     result += "\r\n" + line;
74                 else
75                     break;
76             }
77             session.close();
78             return result;
79
80         } catch (IOException objException) {
81             log.error("Exceptions:",objException);
82         } finally {
83             if (null != session) {
84                 session.close();
85             }
86         }
87         return null;
88     }
89
90     public static String queryInterfaceMac(String name) {
91         String result = sshExecute("ifconfig " + name);
92         return getMacFromResult(result);
93     }
94
95     public static String queryInterfaceMac(String name, String namespce) {
96         String result = sshExecute("ip netns exec " + namespce + " ifconfig " + name);
97         return getMacFromResult(result);
98     }
99
100     private static String getMacFromResult(String result) {
101         String args[] = result.split("\r\n");
102         for (int i = 0; i < args.length; i++) {
103             String arg = args[i].trim();
104             if (arg.contains("HWaddr")) {
105                 String s[] = arg.split(" ");
106                 return s[s.length - 1];
107             }
108         }
109         return null;
110     }
111 }