Run linux SSH command in a plugin.

Discussion in 'Spigot Plugin Development' started by Seeyko, Apr 27, 2017.

  1. Hello all !

    As the title say, i want to run ssh command via a plugin (like when a player clic on an item, it's connect him to the ssh (like ssh [email protected]), then run a code who can execute something like this :
    Code (Text):

    if(ssh.getfolders("/home/server1").exist()){
    executeSSHCommand("cd /home/server1");
    executeSSHCommand("sh startServer1.sh");
    }
    else if(! (ssh.getfolders("/home/server1").exist())){
    executeSSHCommand("mkdir /home/server1");
    executeSSHCommand("cd /home/server1");
    executeSSHCommand("sh startServer1.sh");
    }
     
    So i know it's from java with Process etc... But did you guys have already done this, is this possible like this and did you have any tutorial or whatever who can help me achieve this ? Or lessons of java on this ?

    Thanks !
     
  2. Are you actually trying to access another machine (like real SSH) or just the machine it's running on? If you are trying to do it on the machine you are running on you just do
    Code (Text):
    public static Process doCommand(String command) throws IOException {
        return Runtime.getRuntime().exec(command);
    }

    public static boolean fileExist(String path) {
        return new File(path).exists();
    }
    and to invoke doCommand(String) you just put a try-catch block with this in it. Process is returned so you can bind the IO streams to some other one.

    IF you are trying to do real SSH, I've been trying to do that too. Anyway it's a security flaw. If you are still convinced go look at JSch. They also have some examples with SSH and SFTP
     
  3. Wow thanks men, for the moment it will be on the same machine (not enough money for buying multiple ahah) but soon when i'm going to get my month pay i will try to connect multiple machine (actually it's more multiple vps for the moment but..)
    So for the moment i'm going to try your first code, then i'm going to see with JSch, isaw on other thread this name came often.

    And just for asking, where did you learn this ? on classic online lessons or on forums ?!
     
  4. Well when I was looking for a good SFTP Java implementation and then I found JSch on StackOverflow. I never actually tried it but it looks promising. The System Runtime method (first one) was found then I was just looking through rt.jar library (rt.jar has most of Java's main stuff and some JCE implementations are also here). And also that one might not work because of Java not looking through the path.
     
    • Agree Agree x 1