Search in sources :

Example 31 with Connection

use of org.openmuc.j62056.Connection in project intellij-community by JetBrains.

the class StdoutAndStderr method main.

public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "joe";
    String password = "joespass";
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect */
        conn.connect();
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("echo \"Text on STDOUT\"; echo \"Text on STDERR\" >&2");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        InputStream stderr = new StreamGobbler(sess.getStderr());
        BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
        BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
        System.out.println("Here is the output from stdout:");
        while (true) {
            String line = stdoutReader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        System.out.println("Here is the output from stderr:");
        while (true) {
            String line = stderrReader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Session(com.trilead.ssh2.Session)

Example 32 with Connection

use of org.openmuc.j62056.Connection in project openhab1-addons by openhab.

the class Meter method read.

/**
     * Reads data from meter
     * 
     * @return a map of DataSet objects with the obis as key.
     */
public Map<String, DataSet> read() {
    // the frequently executed code (polling) goes here ...
    Map<String, DataSet> dataSetMap = new HashMap<String, DataSet>();
    Connection connection = new Connection(config.getSerialPort(), config.getInitMessage(), config.getEchoHandling(), config.getBaudRateChangeDelay());
    try {
        try {
            connection.open();
        } catch (IOException e) {
            logger.error("Failed to open serial port {}: {}", config.getSerialPort(), e.getMessage());
            return dataSetMap;
        }
        List<DataSet> dataSets = null;
        try {
            dataSets = connection.read();
            for (DataSet dataSet : dataSets) {
                logger.debug("DataSet: {};{};{}", dataSet.getId(), dataSet.getValue(), dataSet.getUnit());
                dataSetMap.put(dataSet.getId(), dataSet);
            }
        } catch (IOException e) {
            logger.error("IOException while trying to read: {}", e.getMessage());
        } catch (TimeoutException e) {
            logger.error("Read attempt timed out");
        }
    } finally {
        connection.close();
    }
    return dataSetMap;
}
Also used : HashMap(java.util.HashMap) DataSet(org.openmuc.j62056.DataSet) Connection(org.openmuc.j62056.Connection) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 33 with Connection

use of org.openmuc.j62056.Connection in project cachecloud by sohutv.

the class SSHTemplate method getConnection.

/**
     * 获取连接并校验
     * @param ip
     * @param port
     * @param username
     * @param password
     * @return Connection
     * @throws Exception
     */
private Connection getConnection(String ip, int port, String username, String password) throws Exception {
    Connection conn = new Connection(ip, port);
    conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT);
    boolean isAuthenticated = conn.authenticateWithPassword(username, password);
    if (isAuthenticated == false) {
        throw new Exception("SSH authentication failed with [ userName: " + username + ", password: " + password + "]");
    }
    return conn;
}
Also used : Connection(ch.ethz.ssh2.Connection) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) SSHException(com.sohu.cache.exception.SSHException)

Example 34 with Connection

use of org.openmuc.j62056.Connection in project CloudStack-archive by CloudStack-extras.

the class SshTest method main.

public static void main(String[] args) {
    // Parameters
    List<String> argsList = Arrays.asList(args);
    Iterator<String> iter = argsList.iterator();
    while (iter.hasNext()) {
        String arg = iter.next();
        if (arg.equals("-h")) {
            host = iter.next();
        }
        if (arg.equals("-p")) {
            password = iter.next();
        }
        if (arg.equals("-u")) {
            url = iter.next();
        }
    }
    if (host == null || host.equals("")) {
        s_logger.info("Did not receive a host back from test, ignoring ssh test");
        System.exit(2);
    }
    if (password == null) {
        s_logger.info("Did not receive a password back from test, ignoring ssh test");
        System.exit(2);
    }
    try {
        s_logger.info("Attempting to SSH into host " + host);
        Connection conn = new Connection(host);
        conn.connect(null, 60000, 60000);
        s_logger.info("User + ssHed successfully into host " + host);
        boolean isAuthenticated = conn.authenticateWithPassword("root", password);
        if (isAuthenticated == false) {
            s_logger.info("Authentication failed for root with password" + password);
            System.exit(2);
        }
        String linuxCommand = "wget " + url;
        Session sess = conn.openSession();
        sess.execCommand(linuxCommand);
        sess.close();
        conn.close();
    } catch (Exception e) {
        s_logger.error("SSH test fail with error", e);
        System.exit(2);
    }
}
Also used : Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 35 with Connection

use of org.openmuc.j62056.Connection in project CloudStack-archive by CloudStack-extras.

the class guestNetwork method run.

public void run() {
    NDC.push("Following thread has started" + Thread.currentThread().getName());
    int retry = 0;
    //Start copying files between machines in the network
    s_logger.info("The size of the array is " + this.virtualMachines.size());
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
                Thread.sleep(120000);
            }
            for (VirtualMachine vm : this.virtualMachines) {
                s_logger.info("Attempting to SSH into linux host " + this.publicIp + " with retry attempt: " + retry);
                Connection conn = new Connection(this.publicIp);
                conn.connect(null, 600000, 600000);
                s_logger.info("SSHed successfully into linux host " + this.publicIp);
                boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                if (isAuthenticated == false) {
                    s_logger.info("Authentication failed");
                }
                //execute copy command
                Session sess = conn.openSession();
                String fileName;
                Random ran = new Random();
                fileName = Math.abs(ran.nextInt()) + "-file";
                String copyCommand = new String("./scpScript " + vm.getPrivateIp() + " " + fileName);
                s_logger.info("Executing " + copyCommand);
                sess.execCommand(copyCommand);
                Thread.sleep(120000);
                sess.close();
                //execute wget command
                sess = conn.openSession();
                String downloadCommand = new String("wget http://172.16.0.220/scripts/checkDiskSpace.sh; chmod +x *sh; ./checkDiskSpace.sh; rm -rf checkDiskSpace.sh");
                s_logger.info("Executing " + downloadCommand);
                sess.execCommand(downloadCommand);
                Thread.sleep(120000);
                sess.close();
                //close the connection
                conn.close();
            }
        } catch (Exception ex) {
            s_logger.error(ex);
            retry++;
            if (retry == retryNum) {
                s_logger.info("Performance Guest Network test failed with error " + ex.getMessage());
            }
        }
    }
}
Also used : Random(java.util.Random) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Aggregations

Connection (com.trilead.ssh2.Connection)36 Session (com.trilead.ssh2.Session)31 IOException (java.io.IOException)23 InputStream (java.io.InputStream)20 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 Connection (org.jboss.remoting3.Connection)7 SCPClient (com.trilead.ssh2.SCPClient)6 StreamGobbler (com.trilead.ssh2.StreamGobbler)5 Principal (java.security.Principal)4 Connection (okhttp3.Connection)4 Request (okhttp3.Request)4 RequestBody (okhttp3.RequestBody)4 SecurityContext (org.jboss.security.SecurityContext)4 SecurityIdentity (org.wildfly.security.auth.server.SecurityIdentity)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 Charset (java.nio.charset.Charset)3 Headers (okhttp3.Headers)3