Guest User

Untitled

a guest
Nov 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Net::SSH::Expect;
  4. my $ssh = Net::SSH::Expect-> new(
  5. host => "host123",
  6. password => "passwd",
  7. user => "username",
  8. raw_pty => 1,
  9. timeout => 1,
  10. );
  11. $ssh->login();
  12. $ssh->exec("su - root");
  13. $ssh->exec("passwd");
  14. $ssh->exec("su - user");
  15. $ssh->exec("command1);
  16. $ssh->exec("subcommand");
  17. my $output = $ssh->exec("subcommand 2");
  18. warn "$outputn";
  19. $ssh->exec("n");
  20. my $output2 = $ssh->exec("subcommand 3");
  21. warn "$output2n";
  22. $ssh->exec("n")
  23.  
  24. import java.io.InputStreamReader;
  25. import ch.ethz.ssh2.Connection;
  26. import ch.ethz.ssh2.SCPClient;
  27. import ch.ethz.ssh2.Session;
  28. import ch.ethz.ssh2.StreamGobbler;
  29. import java.util.concurrent.TimeUnit;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. public void sshExecute() throws Exception {
  33.  
  34. String username = "username";
  35. String password = "passwd";
  36. String hostname = "host123";
  37. logger.setLevel(Level.INFO);
  38.  
  39. Object lastCommandOutput = null;
  40. logger.info("starting connection with "+hostname);
  41. Connection connection = new Connection(hostname);
  42. logger.info("connection object created..");
  43. connection.connect();
  44. connection.authenticateWithPassword(username, password);
  45. Session session = connection.openSession();
  46. InputStream stdout = new StreamGobbler(session.getStdout());
  47. BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
  48. logger.info("connected");
  49.  
  50. String tempCommand = "su - root";
  51. String Pswd="passwd";
  52. logger.info("sending command: " + tempCommand);
  53.  
  54. session.execCommand(tempCommand + Pswd);
  55. TimeUnit.SECONDS.sleep(5);
  56.  
  57. session.execCommand("su - user");
  58.  
  59. // Get output
  60. StringBuffer sb = new StringBuffer();
  61. while(true){
  62. String line = stdoutReader.readLine();
  63. if(line == null)
  64. break;
  65. sb.append(line+"n");
  66. }
  67. String output = sb.toString();
  68. lastCommandOutput = output;
  69. logger.info("got output: " + output);
  70. stdoutReader.close();
  71. }
  72.  
  73. 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