Sending commands to server via JSch shell channel

后端 未结 9 968
渐次进展
渐次进展 2020-11-30 03:37

I can\'t figure it out how I can send commands via JSch shell channel.

I do this, but it doesn\'t work:

JSch shell = new JSch();
String command = \"c         


        
9条回答
  •  我在风中等你
    2020-11-30 03:46

    Following was a quickly written code for my assignment. Not a well done program. But serves its purpose.

    1. Connects via SSH (using Jsch) to a server (using a Private Key File - mykey.pem)
    2. Creates a shell script (to mount a volume and mkfs)
    3. Runs on the remote machine
    4. All the while you can see the output on your stdout

    The code follows:

    public class connectSSH {
    
    public void connect(String dnsName, String privKey) throws IOException {
        JSch jSch = new JSch();
    
        try {
    
                        //Authenticate through Private Key File
            jSch.addIdentity(privKey);
                        //Give the user and dnsName
            Session session = jSch.getSession("root", dnsName, 22);
                        //Required if not a trusted host
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            System.out.println("Connecting SSH to " + dnsName + " - Please wait for few minutes... ");
            session.connect();
                //Open a shell 
            Channel channel=session.openChannel("shell");
            channel.setOutputStream(System.out);
                //Create a Shell Script
            File shellScript = createShellScript();
                //Convert the shell script to byte stream
            FileInputStream fin = new FileInputStream(shellScript);
            byte fileContent[] = new byte[(int)shellScript.length()];
            fin.read(fileContent);
            InputStream in = new ByteArrayInputStream(fileContent);
                //Set the shell script to the channel as input stream
            channel.setInputStream(in);
                //Connect and have fun!
            channel.connect();          
    
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    public File createShellScript() {
         String filename = "shellscript.sh";
         File fstream = new File(filename);
    
         try{
              // Create file 
             PrintStream out = new PrintStream(new FileOutputStream(fstream));
             out.println("#!/bin/bash");
             out.println("echo \"hi\" > /tmp/test.info");
             out.println("echo \"n\" > /tmp/fdisk.in");
             out.println("echo \"p\" >> /tmp/fdisk.in");
             out.println("echo \"1\" >> /tmp/fdisk.in");
             out.println("echo >> /tmp/fdisk.in");
             out.println("echo >> /tmp/fdisk.in");
             out.println("echo \"w\" >> /tmp/fdisk.in");
    
             out.println("/sbin/fdisk /dev/sdf < /tmp/fdisk.in");
             out.println("mkfs.ext3 /dev/sdf1");
             out.println("mkdir /usr/myebs");
             out.println("mount /dev/sdf1 /usr/myebs");
             out.println("partprobe /dev/sdf1");
    
             out.println("echo \"Success\"");
    
             //Close the output stream
             out.close();
         }catch (Exception e){//Catch exception if any
             System.err.println("Error: " + e.getMessage());
         }
         return fstream;
    
    }
    
    public static void main(String[] args) {
        connectSSH ssh = new connectSSH();
        String privKey = "/Users/neo/Desktop/mykey.pem";
        try {
            ssh.connect("yourexampleserver.com", privKey);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    }
    

提交回复
热议问题