通过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", 22, new 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 core 0 2 9 jar java模拟ssh协议登录
j2ssh-0.2.9-src.zip压缩包是ssh协议安全连接远程服务器的源码,目前是最新版本,有序要的朋友可以下。
SSHTools是一组Java SSH应用程序。包括:Java SSH API, SSH Terminal, SSH secured VNC客户端, SFTP客户端和SSH Daemon。ESftp是根据此项目开发的一个Eclipse插件主要用来在Eclipse开发平台与Sftp服务器之间传输文件...
API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将发布,此旧版API现在已在LGPL ...
J2SSH Maverick是原始J2SSH API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将...
利用Maven整合SSH,包括整合说明以及实例代码,导入Eclipse可直接运行,适合初学SSH开发框架的同学
包括Struts2,spring4.0,hibernate4.2.4等SSH开发需要的jar包
j2cms ssh JPA spring ,包含mysql的sql文件
将src和WebContent目录内容分别拷到工程中对应的目录3.j2cms-ssh.sql是数据库,需要导入MySQL中, 方法一,用MySQLAdministrator导入 方法二,用MySQL命令导入: mysql> create database j2cms-ssh; mysql> use j2cms-...
cms-J2CMS-SSH系统cms-J2CMS-SSH系统cms-J2CMS-SSH系统
ssh模板_j2eeshop是我自己用的一个MVC模板,大家支持下哈,项目里的jar包已经删除了,那个编辑器好像坏了,大家另弄个就行了,不客气哈
struts2 hibernate spring log4j 框架的简单列子 jar.jpg为对应jar包。lib下jar包已去除
SSH框架中配置log4j的方法:在ssh中配置log4j的步骤和方法
在ssh中使用log4j、将日志输出到控制台和文件中。
J2CMS-SSH 纯HTML写的 有数据库的SQL语句
SSH框架整合实战项目,log4j完整的配置文件(log4j.perproties,log4j.xml),日志可输出到控制台、本地文件、数据库,提供多种配置方法,项目可直接运行,SQL语句在项目中。该项目对学习spring,struts2,hibernate,log...