Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Net::SSH::Expect;
- my $ssh = Net::SSH::Expect-> new(
- host => "host123",
- password => "passwd",
- user => "username",
- raw_pty => 1,
- timeout => 1,
- );
- $ssh->login();
- $ssh->exec("su - root");
- $ssh->exec("passwd");
- $ssh->exec("su - user");
- $ssh->exec("command1);
- $ssh->exec("subcommand");
- my $output = $ssh->exec("subcommand 2");
- warn "$outputn";
- $ssh->exec("n");
- my $output2 = $ssh->exec("subcommand 3");
- warn "$output2n";
- $ssh->exec("n")
- import java.io.InputStreamReader;
- import ch.ethz.ssh2.Connection;
- import ch.ethz.ssh2.SCPClient;
- import ch.ethz.ssh2.Session;
- import ch.ethz.ssh2.StreamGobbler;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public void sshExecute() throws Exception {
- String username = "username";
- String password = "passwd";
- String hostname = "host123";
- logger.setLevel(Level.INFO);
- Object lastCommandOutput = null;
- logger.info("starting connection with "+hostname);
- Connection connection = new Connection(hostname);
- logger.info("connection object created..");
- connection.connect();
- connection.authenticateWithPassword(username, password);
- Session session = connection.openSession();
- InputStream stdout = new StreamGobbler(session.getStdout());
- BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
- logger.info("connected");
- String tempCommand = "su - root";
- String Pswd="passwd";
- logger.info("sending command: " + tempCommand);
- session.execCommand(tempCommand + Pswd);
- TimeUnit.SECONDS.sleep(5);
- session.execCommand("su - user");
- // Get output
- StringBuffer sb = new StringBuffer();
- while(true){
- String line = stdoutReader.readLine();
- if(line == null)
- break;
- sb.append(line+"n");
- }
- String output = sb.toString();
- lastCommandOutput = output;
- logger.info("got output: " + output);
- stdoutReader.close();
- }
- java.io.IOException: A remote execution has already started. java.lang.Error: java.io.IOException: A remote execution has already started at com.user1.test.Assert.fail(Assert.java:35)
Add Comment
Please, Sign In to add comment