`
echozhjun
  • 浏览: 47928 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

j2ssh

阅读更多

 通过Ssh协议连接到服务器执行执行的指令。echo $?这条命令将显示上条指令执行的状态。0为成功,其他都是失败。

 

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 import java.io.OutputStream;
 5 
 6 import com.sshtools.j2ssh.SshClient;
 7 import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
 8 import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
 9 import com.sshtools.j2ssh.session.SessionChannelClient;
10 
11 public class Main {
12 
13     public static void main(String[] args) {
14         SshClient ssh = new SshClient();
15         PasswordAuthenticationClient authentication = new PasswordAuthenticationClient();
16         authentication.setUsername("root");
17         authentication.setPassword("123");
18         try {
19             ssh.connect("192.168.94.254"22new HostsKeyVerification());
20             if (ssh.authenticate(authentication) == AuthenticationProtocolState.COMPLETE) {
21                 SessionChannelClient session = ssh.openSessionChannel();
22                 // session.setEnvironmentVariable("TERM", "linux");
23                 // if (client.requestPseudoTerminal("vt100", 120, 400, 0, 0,
24                 // "")) {
25                 if (session.startShell()) {
26                     OutputStream writer = session.getOutputStream();
27                     writer.write("echo $?\n".getBytes());
28                     writer.flush();
29                     writer.write("exit\n".getBytes());
30                     writer.flush();
31                     BufferedReader in = new BufferedReader(
32                             new InputStreamReader(session.getInputStream()));
33                     BufferedReader err = new BufferedReader(
34                             new InputStreamReader(session
35                                     .getStderrInputStream()));
36                     String line;
37                     while ((line = in.readLine()) != null) {
38                         System.out.println(line);
39                     }
40                     System.out.println("------------------------");
41                     while ((line = err.readLine()) != null) {
42                         System.out.println(line);
43                     }
44                     if (session != null) {
45                         session.close();
46                     }
47                 }
48                 // }
49             }
50         } catch (IOException e) {
51             e.printStackTrace();
52         } finally {
53         }
54 
55     }
56 
57 }
58 

 

 

分享到:
评论

相关推荐

    J2SSH(Java SSH API )使用文档

    J2SSH(Java SSH API )使用文档 J2SSH(Java SSH API )使用文档 J2SSH(Java SSH API )使用文档

    j2ssh-core-0.2.9.jar

    j2ssh core 0 2 9 jar java模拟ssh协议登录

    j2ssh-0.2.9-src.zip

    j2ssh-0.2.9-src.zip压缩包是ssh协议安全连接远程服务器的源码,目前是最新版本,有序要的朋友可以下。

    j2ssh-core-0.2.2.jar

    SSHTools是一组Java SSH应用程序。包括:Java SSH API, SSH Terminal, SSH secured VNC客户端, SFTP客户端和SSH Daemon。ESftp是根据此项目开发的一个Eclipse插件主要用来在Eclipse开发平台与Sftp服务器之间传输文件...

    java源码api-j2ssh-maverick:我们的旧版API的开源分支为社区提供了强大的,关键任务SSH组件

    API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将发布,此旧版API现在已在LGPL ...

    SSHTOOLS J2SSH Maverick:J2SSH的原始作者的第二代SSH API-开源

    J2SSH Maverick是原始J2SSH API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将...

    采用Maven整合SSH

    利用Maven整合SSH,包括整合说明以及实例代码,导入Eclipse可直接运行,适合初学SSH开发框架的同学

    SSH项目依赖的jar包

    包括Struts2,spring4.0,hibernate4.2.4等SSH开发需要的jar包

    j2cms内容ssh

    j2cms ssh JPA spring ,包含mysql的sql文件

    J2CMS内容管理系统 SSH 20140806

    将src和WebContent目录内容分别拷到工程中对应的目录3.j2cms-ssh.sql是数据库,需要导入MySQL中, 方法一,用MySQLAdministrator导入 方法二,用MySQL命令导入: mysql&gt; create database j2cms-ssh; mysql&gt; use j2cms-...

    cms-J2CMS-SSH系统

    cms-J2CMS-SSH系统cms-J2CMS-SSH系统cms-J2CMS-SSH系统

    ssh模板_j2eeshop

    ssh模板_j2eeshop是我自己用的一个MVC模板,大家支持下哈,项目里的jar包已经删除了,那个编辑器好像坏了,大家另弄个就行了,不客气哈

    ssh2+log4j+异常简单框架

    struts2 hibernate spring log4j 框架的简单列子 jar.jpg为对应jar包。lib下jar包已去除

    SSH框架中配置log4j的方法

    SSH框架中配置log4j的方法:在ssh中配置log4j的步骤和方法

    ssh中log4j的使用

    在ssh中使用log4j、将日志输出到控制台和文件中。

    J2CMS-SSH.zip

    J2CMS-SSH 纯HTML写的 有数据库的SQL语句

    ssh ,log4j实例

    SSH框架整合实战项目,log4j完整的配置文件(log4j.perproties,log4j.xml),日志可输出到控制台、本地文件、数据库,提供多种配置方法,项目可直接运行,SQL语句在项目中。该项目对学习spring,struts2,hibernate,log...

Global site tag (gtag.js) - Google Analytics