Advertisement
lilggamegenuis

runcmd code

Aug 28th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. // !runcmd - Tells the bot to say run a OS command
  2.         if (arguments[0].equalsIgnoreCase(prefix + "runcmd")){
  3.             if(sender.equalsIgnoreCase("Lil-G")){
  4.                 ProcessBuilder builder = new ProcessBuilder("cmd.exe");
  5.                 Process p=null;
  6.                 try {
  7.                     p = builder.start();
  8.                 }
  9.                 catch (IOException e) {
  10.                     sendMessage(channel, "Error: " + e);
  11.                 }
  12.                 //get stdin of shell
  13.                 BufferedWriter p_stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
  14.  
  15.                 try {
  16.                     //single execution
  17.                     p_stdin.write(arguments[1]);
  18.                     p_stdin.newLine();
  19.                     p_stdin.flush();
  20.                 }
  21.                 catch (IOException e) {
  22.                     sendMessage(channel, "Error: " + e);
  23.                 }
  24.                 // finally close the shell by execution exit command
  25.                 try {
  26.                     p_stdin.write("exit");
  27.                     p_stdin.newLine();
  28.                     p_stdin.flush();
  29.                 }
  30.                 catch (IOException e) {
  31.                     System.out.println(e);
  32.                 }
  33.  
  34.                 // write stdout of shell (=output of all commands)
  35.                 Scanner s = new Scanner( p.getInputStream() );
  36.                 int i = 0;
  37.                 while (s.hasNext())
  38.                 {
  39.                     sendMessage(channel, s.next());
  40.                 }
  41.                 s.close();
  42.             }
  43.             else {
  44.                 permError(sender, "can use this command");
  45.             }
  46.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement