• 远程操作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;
        }

    }

  • 相关阅读:
    Jenkins遇到问题一:jenkins配置权限不对导致无法登陆或者空白页面解决办法
    翻页功能的测试用例
    Jenkins学习二:Jenkins安装与配置
    Jenkins学习一:Jenkins是什么?
    Java MyEclipse下Ant build.xml简单实例详解
    Linux环境中Openfire安装指南
    Linux下查看文件和文件夹大小
    Tsung测试openfire服务器
    Windows环境中Openfire与Spark安装与配置指南
    解决-bash: lsb_release: command not found
  • 原文地址:https://www.cnblogs.com/nicebaby/p/7419194.html
Copyright © 2020-2023  润新知