博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java使用ssh连接Linux并执行命令
阅读量:6868 次
发布时间:2019-06-26

本文共 1792 字,大约阅读时间需要 5 分钟。

 

maven pom.xml配置:
        
com.jcraft
           
jsch
           
0.1.54
    
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;    } }

 

 原文:

 

转载于:https://www.cnblogs.com/xiaoliu66007/p/11084208.html

你可能感兴趣的文章
小措施提高Linux服务器安全
查看>>
数据一致性
查看>>
linux大于2T硬盘分区方法
查看>>
ISAPI_Rewrite 1.3 版本做301转向的问题
查看>>
Lucene In Action 读书笔记(一)
查看>>
iOS 9适配技巧(更新版)
查看>>
实时群聊小程序开发记录
查看>>
Python 数据库备份脚本(邮件通知)
查看>>
学习SDL的一些资料(整理)
查看>>
[动态库]深入分析Windows和Linux动态库应用异同
查看>>
Linux下查看CPU信息、机器型号等硬件信息命令
查看>>
Lync Server 2013 部署 _ 部署简介及系统要求
查看>>
前端小随笔
查看>>
view属性大全
查看>>
xshell如何保存日志
查看>>
删除遗留在系统的旧网卡设备驱动
查看>>
redhat6.1安装oracle11g(新手,搞了7天)
查看>>
PHP与MySQL通讯那点事
查看>>
Java文件编码示例
查看>>
CactiFans V1.0中文版发布
查看>>