zoukankan      html  css  js  c++  java
  • java使用ssh连接Linux并执行命令

     方式1:通过设置账号密码和链接地址

    maven pom.xml配置:
    
    <dependency>
            <groupId>com.jcraft</groupId>
               <artifactId>jsch</artifactId>
               <version>0.1.54</version>
        </dependency>
    java代码如下:
    import java.io.IOException;
    import java.io.InputStream;
    import org.apache.commons.io.IOUtils; 
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
     
    public class SSHLinux { 
        public static void main(String[] args) throws IOException, JSchException {
            // TODO Auto-generated method stub
            String host = "172.19.28.253";
            int port = 22;
            String user = "root";
            String password = "123456";
            String command = "whatweb --output-xml http://216.139.147.75:443/";
            String res = exeCommand(host,port,user,password,command); 
            System.out.println(res);        
        }
    public static String exeCommand(String host, int port, String user, String password, String command) throws JSchException, IOException {        
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
        //    java.util.Properties config = new java.util.Properties();
         //   config.put("StrictHostKeyChecking", "no");        
            session.setPassword(password);
            session.connect();
            
            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
            InputStream in = channelExec.getInputStream();
            channelExec.setCommand(command);
            channelExec.setErrStream(System.err);
            channelExec.connect();
            String out = IOUtils.toString(in, "UTF-8");
    channelExec.disconnect(); session.disconnect();
    return out; } }

     原文:java使用ssh连接Linux并执行命令

     方式2:通过获取linux当前环境

    linux下:

    String[] command = { "/bin/sh", "-c", cmmd };
    Process ps = Runtime.getRuntime().exec(command );

    windows下: 

    String[] command = { "cmd", "/c", cmmd };
    Process ps = Runtime.getRuntime().exec(command );

    e.g.

    import org.apache.commons.io.IOUtils;
    import java.io.IOException;
    import java.util.*;
    public class ISshServerRuntimeImpl implements ISshServerRuntime{
        public static void main(String[] args) throws IOException, JSchException {
            String[] cmdarray = { "cmd", "/c","notepad.exe"};
            final Process p = Runtime.getRuntime().exec(cmdarray);
            String res = IOUtils.toString(p.getInputStream());
        }
    }
  • 相关阅读:
    冒泡 希尔 快速 插入 堆 基数
    排序总结
    软件工程(齐治昌-谭庆平-宁洪)
    Java简单计算器
    插入排序
    Android中theme.xml与style.xml的区别
    activity theme parent 属性浅析
    xml中不能直接添加ViewGroup
    Java中对象的上转型对象
    Android原理View、ViewGroup
  • 原文地址:https://www.cnblogs.com/xiaoliu66007/p/11084208.html
Copyright © 2011-2022 走看看