zoukankan      html  css  js  c++  java
  • 远程操作linux

    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;
        }

    }

  • 相关阅读:
    矩阵微分
    Installing a single-server IBM FileNet P8 Platform system with IBM Content Navigator
    Linux创建LVM
    tomcat 集群配置,Session复制共享
    JBoss JMX登录需要用户名密码的解决办法
    JBOss启动只能在本机访问的解决办法
    SSH由WAS/Tomcat/Weblogic迁移到JBOSS
    Log4J实用配置指南
    Graphical installers are not supported by the vm
    在vim中执行外部命令
  • 原文地址:https://www.cnblogs.com/nicebaby/p/7419194.html
Copyright © 2011-2022 走看看