- 浏览: 387739 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
Nabulio:
写的详细,特殊语法学习到了
jdk1.5-1.9新特性 -
wooddawn:
您好,最近在做个足球数据库系统,用到了betbrain的数据表 ...
javascript深入理解js闭包 -
lwpan:
很受启发 update也可以
mysql 的delete from 子查询限制 -
wuliaolll:
不错,总算找到原因了
mysql 的delete from 子查询限制
工作中遇到的问题:
Java code运行在一台机器上,需要远程到linux的机器同时执行多种命令。原来采用的方法是直接调用ssh命令或者调用plink的命令。
google下java的其他ssh方法,发现有个包。
具体介绍如下:
Ganymed SSH2 for Java is a library which implements the SSH-2 protocol in pure Java.It allows one to connect to SSH servers from within Java programs. It supports SSH sessions(remote command execution and shell access),local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP.There are no dependencies on any JCE provider, as all crypto functionality is included.
Ganymed SSH2 for Java was first developed for the Ganymed replication project and acouple of other projects at the IKS group at ETH Zurich.
Website: http://www.cleondris.ch/opensource/ssh2/.
给个链接:http://www.cleondris.ch/opensource/ssh2/javadoc/
更多关于ssh协议的介绍可以查看http://www.ietf.org/rfc/rfc4251.txt
包中主要的类:
Package ch.ethz.ssh2
Interface Summary
ChannelCondition Contains constants that can be used to specify what conditions to wait for on a SSH-2 channel (e.g., represented by aSession).
ConnectionMonitor A ConnectionMonitor is used to get notified when the underlying socket of a connection is closed.
InteractiveCallback An InteractiveCallback is used to respond to challenges sent by the server if authentication mode "keyboard-interactive" is selected.
ProxyData An abstract marker interface implemented by all proxy data implementations.
ServerHostKeyVerifier A callback interface used to implement a client specific method of checking server host keys.
Class Summary
Connection A Connection is used to establish an encrypted TCP/IP connection to a SSH-2 server.
ConnectionInfo In most cases you probably do not need the information contained in here.
DHGexParameters A DHGexParameters object can be used to specify parameters for the diffie-hellman group exchange.
HTTPProxyData A HTTPProxyData object is used to specify the needed connection data to connect through a HTTP proxy.
KnownHosts The KnownHosts class is a handy tool to verify received server hostkeys based on the information inknown_hosts files (the ones used by OpenSSH).
LocalPortForwarder A LocalPortForwarder forwards TCP/IP connections to a local port via the secure tunnel to another host (which may or may not be identical to the remote SSH-2 server).
LocalStreamForwarder A LocalStreamForwarder forwards an Input- and Outputstream pair via the secure tunnel to another host (which may or may not be identical to the remote SSH-2 server).
SCPClient A very basic SCPClient that can be used to copy files from/to the SSH-2 server.
Session A Session is a remote execution of a program.
SFTPv3Client A SFTPv3Client represents a SFTP (protocol version 3) client connection tunnelled over a SSH-2 connection.
SFTPv3DirectoryEntry A SFTPv3DirectoryEntry as returned by SFTPv3Client.ls(String).
SFTPv3FileAttributes A SFTPv3FileAttributes object represents detail information about a file on the server.
SFTPv3FileHandle A SFTPv3FileHandle.
StreamGobbler A StreamGobbler is an InputStream that uses an internal worker thread to constantly consume input from another InputStream.
Exception Summary
HTTPProxyException May be thrown upon connect() if a HTTP proxy is being used.
SFTPException Used in combination with the SFTPv3Client.
给个例子:
(1) SSH到Linux机器
package ssh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.*;
public class Basic {
public static void main(String[] args)
{
String hostname ="9.12.175.30";
String username="root";
String password="password";
try{
//建立连接
Connection conn= new Connection(hostname);
System.out.println("set up connections");
conn.connect();
//利用用户名和密码进行授权
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if(isAuthenticated ==false)
{
throw new IOException("Authorication failed");
}
//打开会话
Session sess = conn.openSession();
System.out.println("Execute command:/usr/bin/perl /test/discover.pl /test/meps_linux.txt");
//执行命令
sess.execCommand("/usr/bin/perl /test/discover.pl /test/meps_linux.txt");
System.out.println("The execute command output is:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while(true)
{
String line = br.readLine();
if(line==null) break;
System.out.println(line);
}
System.out.println("Exit code "+sess.getExitStatus());
sess.close();
conn.close();
System.out.println("Connection closed");
}catch(IOException e)
{
System.out.println("can not access the remote machine");
}
}
}
(2) SSH到windows机器:
windows由于没有默认的ssh server,因此在允许ssh之前需要先安装ssh server。
下载freeSSHd
http://www.freesshd.com/?ctt=download
安装
直接执行freeSSHd.exe就可以进行安装了(用户一定要有管理员权限),安装过程中freeSSHd会问你
Private keys should be created. Should I do it now?
这是问你是否需要现在创建私钥,回答是
Do you want to run FreeSSHd as a system service?
这是问你是否希望把freeSSHd作为系统服务启动,回答是之后就安装完成了
配置用户名和密码:
用putty测试连接。
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\WINDOWS\system32>
例子一中需要两行代码进行修改:
String hostname ="localhost";
sess.execCommand("cmd");
输出结果:
set up connections
Begin execute remote commands
The execute command output is:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
如果要在cmd中执行其他命令,如perl
则使用:
sess.execCommand("cmd.exe /c \"perl -V\"");
cmd /c dir 是执行完dir命令后关闭命令窗口。
cmd /k dir 是执行完dir命令后不关闭命令窗口。
cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。
cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。
可以用cmd /?查看帮助信息。
说白了感觉java的ssh就是一个client
Java code运行在一台机器上,需要远程到linux的机器同时执行多种命令。原来采用的方法是直接调用ssh命令或者调用plink的命令。
google下java的其他ssh方法,发现有个包。
具体介绍如下:
Ganymed SSH2 for Java is a library which implements the SSH-2 protocol in pure Java.It allows one to connect to SSH servers from within Java programs. It supports SSH sessions(remote command execution and shell access),local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP.There are no dependencies on any JCE provider, as all crypto functionality is included.
Ganymed SSH2 for Java was first developed for the Ganymed replication project and acouple of other projects at the IKS group at ETH Zurich.
Website: http://www.cleondris.ch/opensource/ssh2/.
给个链接:http://www.cleondris.ch/opensource/ssh2/javadoc/
更多关于ssh协议的介绍可以查看http://www.ietf.org/rfc/rfc4251.txt
包中主要的类:
Package ch.ethz.ssh2
Interface Summary
ChannelCondition Contains constants that can be used to specify what conditions to wait for on a SSH-2 channel (e.g., represented by aSession).
ConnectionMonitor A ConnectionMonitor is used to get notified when the underlying socket of a connection is closed.
InteractiveCallback An InteractiveCallback is used to respond to challenges sent by the server if authentication mode "keyboard-interactive" is selected.
ProxyData An abstract marker interface implemented by all proxy data implementations.
ServerHostKeyVerifier A callback interface used to implement a client specific method of checking server host keys.
Class Summary
Connection A Connection is used to establish an encrypted TCP/IP connection to a SSH-2 server.
ConnectionInfo In most cases you probably do not need the information contained in here.
DHGexParameters A DHGexParameters object can be used to specify parameters for the diffie-hellman group exchange.
HTTPProxyData A HTTPProxyData object is used to specify the needed connection data to connect through a HTTP proxy.
KnownHosts The KnownHosts class is a handy tool to verify received server hostkeys based on the information inknown_hosts files (the ones used by OpenSSH).
LocalPortForwarder A LocalPortForwarder forwards TCP/IP connections to a local port via the secure tunnel to another host (which may or may not be identical to the remote SSH-2 server).
LocalStreamForwarder A LocalStreamForwarder forwards an Input- and Outputstream pair via the secure tunnel to another host (which may or may not be identical to the remote SSH-2 server).
SCPClient A very basic SCPClient that can be used to copy files from/to the SSH-2 server.
Session A Session is a remote execution of a program.
SFTPv3Client A SFTPv3Client represents a SFTP (protocol version 3) client connection tunnelled over a SSH-2 connection.
SFTPv3DirectoryEntry A SFTPv3DirectoryEntry as returned by SFTPv3Client.ls(String).
SFTPv3FileAttributes A SFTPv3FileAttributes object represents detail information about a file on the server.
SFTPv3FileHandle A SFTPv3FileHandle.
StreamGobbler A StreamGobbler is an InputStream that uses an internal worker thread to constantly consume input from another InputStream.
Exception Summary
HTTPProxyException May be thrown upon connect() if a HTTP proxy is being used.
SFTPException Used in combination with the SFTPv3Client.
给个例子:
(1) SSH到Linux机器
package ssh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.*;
public class Basic {
public static void main(String[] args)
{
String hostname ="9.12.175.30";
String username="root";
String password="password";
try{
//建立连接
Connection conn= new Connection(hostname);
System.out.println("set up connections");
conn.connect();
//利用用户名和密码进行授权
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if(isAuthenticated ==false)
{
throw new IOException("Authorication failed");
}
//打开会话
Session sess = conn.openSession();
System.out.println("Execute command:/usr/bin/perl /test/discover.pl /test/meps_linux.txt");
//执行命令
sess.execCommand("/usr/bin/perl /test/discover.pl /test/meps_linux.txt");
System.out.println("The execute command output is:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while(true)
{
String line = br.readLine();
if(line==null) break;
System.out.println(line);
}
System.out.println("Exit code "+sess.getExitStatus());
sess.close();
conn.close();
System.out.println("Connection closed");
}catch(IOException e)
{
System.out.println("can not access the remote machine");
}
}
}
(2) SSH到windows机器:
windows由于没有默认的ssh server,因此在允许ssh之前需要先安装ssh server。
下载freeSSHd
http://www.freesshd.com/?ctt=download
安装
直接执行freeSSHd.exe就可以进行安装了(用户一定要有管理员权限),安装过程中freeSSHd会问你
Private keys should be created. Should I do it now?
这是问你是否需要现在创建私钥,回答是
Do you want to run FreeSSHd as a system service?
这是问你是否希望把freeSSHd作为系统服务启动,回答是之后就安装完成了
配置用户名和密码:
用putty测试连接。
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\WINDOWS\system32>
例子一中需要两行代码进行修改:
String hostname ="localhost";
sess.execCommand("cmd");
输出结果:
set up connections
Begin execute remote commands
The execute command output is:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
如果要在cmd中执行其他命令,如perl
则使用:
sess.execCommand("cmd.exe /c \"perl -V\"");
cmd /c dir 是执行完dir命令后关闭命令窗口。
cmd /k dir 是执行完dir命令后不关闭命令窗口。
cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。
cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。
可以用cmd /?查看帮助信息。
说白了感觉java的ssh就是一个client
发表评论
-
将json格式的字符数组转为List对象
2015-08-10 15:18 898使用的是json-lib.jar包 将json格式的字符数组 ... -
用httpPost对JSON发送和接收的例子
2015-08-10 11:16 1096HTTPPost发送JSON: private static ... -
zookeeper适用场景:zookeeper解决了哪些问题
2015-07-31 18:01 752问题导读: 1.master挂机 ... -
java泛型
2015-07-29 10:48 762什么是泛型? 泛型(Ge ... -
Java线程Dump分析工具--jstack
2015-06-23 11:09 710jstack用于打印出给定的java进程ID或core fil ... -
什么是spark?
2015-04-10 09:37 485关于Spark: Spark是UC Berkeley AM ... -
dubbo 教程
2015-04-09 19:21 771先给出阿里巴巴dubbo的 ... -
jre/bin目录下面工具说明
2015-03-20 16:45 634jre/bin目录下面工具说明 ... -
JVM系列三:JVM参数设置、分析
2015-01-30 11:18 692不管是YGC还 ... -
jstat使用
2015-01-27 11:11 674jstat 1. jstat -gc pid ... -
查看java堆栈情况(cpu占用过高)
2015-01-27 11:10 7361. 确定占用cpu高的线程id: 方法一: 直接使用 ps ... -
慎用ArrayList的contains方法,使用HashSet的contains方法代替
2015-01-20 14:14 1141在启动一个应用的时候,发现其中有一处数据加载要数分钟,刚开始 ... -
Java虚拟机工作原理详解
2015-01-16 10:00 716一、类加载器首先来 ... -
jdk1.5-1.9新特性
2014-11-11 10:22 83091.51.自动装箱与拆箱:2.枚举(常用来设计单例模式 ... -
java动态代理(JDK和cglib)
2014-09-24 15:51 470JAVA的动态代理 代理模式 代理模式是常用的java设计 ... -
Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
2014-09-24 15:45 690class文件简介及加载 Java编译器编译 ... -
怎么用github下载资源
2014-09-24 11:18 4501、下载github:到http://windows. ... -
maven项目时jar包没有到lib目录下
2014-09-01 20:05 2549在建项目时路径都设置好了,为什么在eclipse中运行mav ... -
使用并行计算大幅提升递归算法效率
2014-08-27 15:04 603前言: 无论什么样的 ... -
JAVA 实现FTP
2014-08-22 14:41 701一个JAVA 实现FTP功能的代码,包括了服务器的设置模块, ...
相关推荐
在Java编程环境中,有时我们需要在Windows系统中远程访问Linux服务器以获取或操作文件。`JSch`库提供了一个这样的解决方案,它是一个纯Java实现的SSH2库,允许开发者连接到远程计算机并执行命令,传输文件等。本篇将...
在Linux系统中,常用的SSH工具包括`ssh`命令行工具和图形化的终端模拟器,如`PuTTY`(主要面向Windows用户)。下面将详细介绍这两个工具的使用方法: 1. **ssh命令行工具**:对于熟悉命令行的用户,`ssh`是首选的...
综上所述,从Windows系统导出XML文件至Linux服务器涉及到Java中的路径处理、文件操作、网络I/O和远程文件系统访问等多个知识点。通过熟练掌握这些技术,开发者可以实现跨平台的数据交换,提高系统的灵活性和兼容性。
PuTTY是一个SSH(Secure Shell)和telnet客户端,广泛用于Windows用户访问基于Unix/Linux的操作系统。它提供了安全的终端模拟,允许用户通过网络进行命令行交互,执行各种管理任务,如文件传输、命令执行等。 PuTTY...
Java FTP工具类是一种在Java编程环境中实现FTP(文件传输协议)功能的类库,它使得在Windows和Linux操作系统上进行文件传输变得简单易行。FTP是互联网上用于在客户端和服务器之间交换文件的标准协议,而Java FTP工具...
3. 安全组策略:确保安全组配置允许SSH访问,以便远程连接ECS实例。 4. 系统优化:根据实际需求调整Java的JVM参数,提高性能和资源利用率。 四、部署Spring Boot应用 1. 编译Spring Boot项目:在本地开发环境中...
首先,Java程序需要识别运行的操作系统类型,因为不同的操作系统(如Windows和Linux)提供了不同的API或系统调用来访问CPU占用信息。在提供的代码示例中,`System.getProperty("os.name")`用于获取操作系统名称,...
.NET Core是其跨平台的开源版本,可以运行在Windows、Linux和macOS上。ASP.NET是.NET框架的一部分,用于构建Web应用程序,而Xamarin则是.NET用于移动开发的解决方案。 Java,作为一种广泛应用的编程语言,以其“一...
在"Linux嵌入式编程必备软件"中,SSH Secure Shell Client是一个客户端程序,如文件中的"SSHSecureShellClient-3.2.9.exe"所示,它允许用户通过加密连接访问远程Linux系统,执行命令、传输文件或管理设备。SSH是...
5. **监控SSH日志**: 监控`/var/log/auth.log`(Linux)或 `%SystemRoot%\System32\winevt\Logs\System.evtx`(Windows),及时发现异常登录尝试。 通过以上步骤,你可以在各种操作系统上成功搭建并优化SSH2环境,实现...
SSH通常用于Linux和Unix-like系统,但也适用于其他平台,包括Windows。在这个“远程连接工具SSH”的绿色版客户端中,用户无需安装,解压后即可直接使用,非常方便。 SSH的工作原理主要分为两个部分:客户端和服务器...
在IT行业中,Linux远程访问工具对于系统管理员和开发者来说至关重要,因为它们允许用户在本地计算机上操作远端Linux服务器,而无需物理访问这些服务器。在Windows操作系统中,有两个常用的工具,即PuTTY和WinSCP,...
例如,操作系统选择 Linux 或 Windows 2003 server 作为测试环境,而生产系统则使用 Linux Red Hat 和特定的服务如 Tomcat 或 Websphere。数据库选用 Oracle 10,并要求使用 JDK 1.6 版本。此外,服务层包括 Tomcat...
【从Windows转向Linux教程V2】是一份专为初次接触Linux操作系统的Windows用户设计的详尽指南。这份教程旨在帮助用户逐步理解Linux的基础知识,掌握从安装到日常使用的各项技能,实现平滑过渡。 首先,教程会讲解...
Raanan Zion https://au.linkedin.com/pub/raanan-zion/88/7b9/255 ssh用于Unix,Linux和MS Windows系统管理和监视的Java接口。 自动执行防火墙规则检查; 将结果导出到Excel。 允许您同时在多个服务器上运行多个...
在Linux或Unix系统中,OpenSSH是最常见的SSH实现;在Windows系统中,PuTTY是一个常用的SSH客户端。 3. 公钥认证:SSH的主要安全特性之一是支持公钥/私钥对的身份验证。用户可以在服务器上设置公钥,然后用私钥进行...
在Windows环境下,你可以使用`scp`(Secure Copy)命令通过SSH(Secure SHell)协议将WAR文件传输到Linux服务器。首先,确保在Windows上安装了PuTTY或类似工具,该工具通常包含`plink.exe`(用于命令行的SSH连接)...
SSH通常被用作Linux和Unix系统中的标准远程访问工具,但也可以通过各种软件实现与其他操作系统如Windows的集成。在本文中,我们将深入探讨SSH的基本概念、工作原理以及如何进行SSH整合。 ### SSH基础 SSH通过加密...
根据提供的文件信息,可以看出这里似乎存在一定的混淆,因为文件标题和描述强调的是SSH配置过程,但实际内容却涉及到了Struts2、Spring以及Hibernate等Java Web框架和技术的配置。为了符合您的需求,我将集中讨论SSH...