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

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

hot3.png

java 里面的开源 ssh lib

1、

2、

JSCH 里面的概念

1、Linux OpenSSH 验证方式对应的 jsch auth method

/etc/sshd_config 文件中

# Authentication:PubkeyAuthentication         //对应的是 publickey 公钥认证PasswordAuthentication yes   //对应的是 password 密码验证ChallengeResponseAuthentication yes  //对应的是 keyboard-interactive 键盘交互# Kerberos options             KerberosAuthentication yes  //对应的是kerberos 验证#KerberosOrLocalPasswd yes#KerberosTicketCleanup yes#KerberosGetAFSToken no# GSSAPI optionsGSSAPIAuthentication yes   //对应的 gssapi-with-mic 验证#GSSAPICleanupCredentials yes#GSSAPIStrictAcceptorCheck yes#GSSAPIKeyExchange no

OpenSHH 文档中写到

The methods available for authentication are: GSSAPI-based authentication, host-based authentication, public key authentication, challenge-response authentication, and password authentication. Authentication methods are tried in the order specified above, thoughPreferredAuthentications can be used to change the default order.

我们在使用jsch 的时候就要注意几点

//StrictHostKeyChecking 选项可用于控制对主机密钥未知或已更改的计算机的登录。session.setConfig("StrictHostKeyChecking", "no");//设置首选的Auth Methodsession.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");

我们如果使用sshj 就可以这样

//调用 sshClient 的这个方法,里面可以实现多个验证方式public void auth(String username, AuthMethod... methods)            throws UserAuthException, TransportException {        checkConnected();        auth(username, Arrays.
asList(methods)); }//例子 DefaultConfig defaultConfig = new DefaultConfig(); final SSHClient client = new SSHClient(defaultConfig); String host = "127.0.0.1"; String user = "king"; String password = "123456"; client.setTimeout(60000); client.loadKnownHosts(); client.addHostKeyVerifier(new PromiscuousVerifier()); client.connect(host); PasswordFinder pwdf = PasswordUtils.createOneOff(password.toCharArray()); PasswordResponseProvider provider = new PasswordResponseProvider(pwdf); //键盘交互 AuthKeyboardInteractive authKeyboardInteractive = new AuthKeyboardInteractive(provider); //密码验证 AuthPassword authPassword = new AuthPassword(pwdf); client.auth(user, authKeyboardInteractive, authPassword);

jsch 例子

JSch jSch = new JSch();		//设置JSch 的日志,可以看到具体日志信息        JSch.setLogger(new Logger() {            @Override            public boolean isEnabled(int level) {                return true;            }            @Override            public void log(int level, String message) {                System.out.println("logger:" + message);            }        });        com.jcraft.jsch.Session session = jSch.getSession("king", "127.0.0.1");        session.setPassword("123456");		//忽略第一次连接时候 hostkey 检查        session.setConfig("StrictHostKeyChecking", "no");        //设置首选的身份验证方式        session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");        session.connect(60000);		//开启shell,shell 具有上下文交互,执行命令不会马上退出        ChannelShell shell = (ChannelShell) session.openChannel("shell");		//开始 exec 类似linux bash -c exec 执行完命令马上退出        //ChannelExec exec = (ChannelExec)session.openChannel("exec");        //exec.setCommand("");        shell.setPtyType("dumb");        shell.setPty(true);        shell.connect(60000);        boolean connected = shell.isConnected();        OutputStream outputStream = shell.getOutputStream();        InputStream inputStream = shell.getInputStream();        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));		//通过流写入命令        outputStream.write("pwd\n cd /home\nls\npwd\n".getBytes());        outputStream.flush();        String line;        while ((line = bufferedReader.readLine()) != null) {            System.out.println(line);        }

sshj 例子

DefaultConfig defaultConfig = new DefaultConfig();        final SSHClient client = new SSHClient(defaultConfig);        String host = "127.0.0.1";        String user = "king";        String password = "123456";        client.setTimeout(60000);        client.loadKnownHosts();        client.addHostKeyVerifier(new PromiscuousVerifier());        client.connect(host);        try {            client.authPassword(user, password);            final SessionChannel session = (SessionChannel) client.startSession();            session.allocateDefaultPTY();			//这里的session 类似 jsch 里面的exec ,可以直接执行命令。			//session.exec("pwd");            SessionChannel shell = (SessionChannel) session.startShell();            try {                OutputStream outputStream = shell.getOutputStream();                outputStream.write("pwd\n".getBytes());                outputStream.flush();                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(shell.getInputStream(), "utf-8"));                String line;                while ((line = bufferedReader.readLine()) != null) {                    System.out.println(line);                }            } catch (InterruptedException e) {                e.printStackTrace();            }        } finally {            client.disconnect();        }

转载于:https://my.oschina.net/u/1266813/blog/2991615

你可能感兴趣的文章
Struts2----><s:token />标签防止重复提交
查看>>
mapreduce (一) 物理图解+逻辑图解
查看>>
自动化测试 Windows 8 应用
查看>>
[译]Array.prototype.concat不是通用方法
查看>>
DropDownList 发现
查看>>
SQL SERVER 2000数据库置疑处理
查看>>
Android系统中的广播(Broadcast)机制简要介绍和学习计划
查看>>
A Theoretical Analysis of Feature Pooling in Visual Recognition
查看>>
【转】耐心看
查看>>
hdu 1272 小希的迷宫
查看>>
原创教程:SpagoBI4.2汉化及配置Mysql数据库教程
查看>>
大写中文数字-財务
查看>>
用Easing函数实现碰撞效果
查看>>
vc++基础班[23]---文件夹的基本操作
查看>>
关于gnome
查看>>
LSPCI具体解释分析
查看>>
【AngularJS】—— 3 我的第一个AngularJS小程序
查看>>
FireFox 浏览器插件/扩展开发学习
查看>>
Groovy 与 Python 的差异【翻译】
查看>>
Theano学习笔记(一)——代数
查看>>