Bug 5077: Codes break the security rules
[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
21 /**
22  * Created by hj on 12/9/15.
23  */
24 public class CmdExecutor {
25     private static Logger log = LoggerFactory.getLogger(CmdExecutor.class);
26     private static Connection sshConnection;
27     private static String strHostName = Config.getHostName();
28     private static String strUserName = Config.getHostUser();
29     private static String strPassword = Config.getHostPassword();
30
31     public static boolean open() {
32         try {
33             sshConnection = new Connection(strHostName);
34             sshConnection.connect();
35             if (false == sshConnection.authenticateWithPassword(strUserName, strPassword)) {
36                 throw new IOException("Authentication failed!");
37             } else {
38                 return true;
39             }
40         } catch (IOException objException) {
41 //            objException.printStackTrace();
42             log.error(objException);
43             if (null != sshConnection) {
44                 sshConnection.close();
45                 sshConnection = null;
46             }
47         }
48         return false;
49     }
50
51     public static void close() {
52         if (null != sshConnection) {
53             sshConnection.close();
54             sshConnection = null;
55         }
56     }
57
58     public static String sshExecute(String command) {
59         Session session = null;
60         try {
61             session = sshConnection.openSession();
62             if (!"root".equals(Config.getHostName())) {
63                 command = "sudo " + command;
64             }
65             session.execCommand(command);
66             InputStream stdout = new StreamGobbler(session.getStdout());
67             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stdout));
68             String result = "";
69             while (true) {
70                 String line = bufferedReader.readLine();
71                 if (line != null)
72                     result += "\r\n" + line;
73                 else
74                     break;
75             }
76             session.close();
77             return result;
78
79         } catch (IOException objException) {
80 //            objException.printStackTrace();
81             log.error(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 }