jsch

JDBC通过SSH Tunnel连接MySQL数据库

﹥>﹥吖頭↗ 提交于 2019-11-30 22:58:27
有时候我们无法直接访问某台数据库,因为没有授权或者ip限制,但是可以通过登陆其他机器来访问,如果这台服务器安装有SSH,就可以方便的在本地通过该服务的端口映射来代理访问数据库。Navicat就有这个方便的功能,如下图所示: 由此联想到,在Java代码中能否实现类似的功能呢? 参考OSC上的这个问题: http://www.oschina.net/question/125831_79245 红薯已经给出了链接,当然,在google上搜索排第一的也是stackoverflow上的回答: http://sina.lt/gUe SSH自带有端口转发的命令,可将本地的任意可用端口转发到远程服务器的其他端口: ssh - L 1234 : localhost : 3306 mysql . server . remote JScH 就是一个实现了SSH2协议的Java包,示例代码如下: import java.sql.*; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class TestJSch { static int lport = 3306;//本地端口 static String rhost = "10.10.10.10";//远程MySQL服务器 static int rport = 3306;

java 通过ssh 执行命令

烈酒焚心 提交于 2019-11-30 22:57:52
java 里面的开源 ssh lib 1、 Jsch 2、 SSHJ 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 options GSSAPIAuthentication yes //对应的 gssapi-with-mic 验证 #GSSAPICleanupCredentials yes #GSSAPIStrictAcceptorCheck yes #GSSAPIKeyExchange no OpenSHH 文档中写到 The

How to remove ANSI control chars (VT100) from a Java String

天大地大妈咪最大 提交于 2019-11-30 22:26:24
I am working with automation and using Jsch to connect to remote boxes and automate some tasks. I am having problem parsing the command results because sometimes they come with ANSI Control chars . I've already saw this answer and this other one but it does not provide any library to do that. I don't want to reinvent the wheel, if there is any. And I don't feel confident with those answers. Right now, I am trying this, but I am not really sure it's complete enough. reply = reply.replaceAll("\\[..;..[m]|\\[.{0,2}[m]|\\(Page \\d+\\)|\u001B\\[[K]|\u001B|\u000F", ""); How to remove ANSI control

Downloading files from an SFTP server using JSch

泪湿孤枕 提交于 2019-11-30 18:29:18
I am using jsch to download files from server, my code below. public static void downloadFile(TpcCredentialsDTO dto) { logger.trace("Entering downloadFile() method"); Session session = null; Channel channel = null; ChannelSftp channelSftp = null; boolean success = false; try { JSch jsch = new JSch(); session = jsch.getSession(dto.getUsername(), dto.getHost(), dto.getPort()); session.setPassword(dto.getPassword()); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); logger.info("Connected to " + dto.getHost() + "."); channel = session.openChannel("sftp"); channel.connect();

Is there any way of having maven scp wagon work consistently on linux/mac/windows platform?

北城以北 提交于 2019-11-30 15:09:57
问题 Given the very poor documentation about scp/ssh and maven I tried different approaches, basically falling in two main categories: using scpexe wagon and scp wagon. Usually they both work without issue on both linux and mac, but on windows I never found a way to make it work on all machines. scpexe approach (after installing complete putty and adding to path) - settings.xml configuration: <server> <id>internal</id> <username>******</username> <password>*******</password> <configuration>

Jsch - One Session Multiple Channels

◇◆丶佛笑我妖孽 提交于 2019-11-30 10:28:23
I managed to execute a single command through ssh with Jsch but when i try to execute a second command it fails For debugging i brought this problem down to this lines: import java.io.IOException; import java.io.InputStream; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; public class Exec { public static void test(Session session) throws Exception { Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand("pwd"); channel.setInputStream(null); (

JSch getting “invalid privatekey:” while trying to load an RSA private key by KeyPairGenerator

[亡魂溺海] 提交于 2019-11-30 08:44:17
问题 I'm using java.security.KeyPairGenerator to gen an RSA key pair, and then try to load the private key via the KeyPair class provided in Jsch(0.1.49). The code: public static void main(String[] args) { String header = "-----BEGIN RSA PRIVATE KEY-----"; String footer = "-----END RSA PRIVATE KEY-----"; KeyPairGenerator keyPairGenerator; try { keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048, new SecureRandom()); PrivateKey privateKey = keyPairGenerator

“reject HostKey” when connecting to remote host through jumphost with JSch

有些话、适合烂在心里 提交于 2019-11-30 05:12:37
问题 Need to SSH to destination host through jumphost. Had tried the same mentioned in JSch JumpHosts example. Session[] sessions = new Session[2]; Session session = null; sessions[0] = session = jsch.getSession(getUserName(), "jumphost1.com", 22); session.setPassword(getHostPassword()); UserInfo userInfo = new UserInfo(); userInfo.setPassword(getHostPassword()); session.setUserInfo(userInfo); Properties prop = new Properties(); prop.put("StrictHostKeyChecking", "no"); prop.put(

Downloading files from an SFTP server using JSch

浪尽此生 提交于 2019-11-30 01:43:30
问题 I am using jsch to download files from server, my code below. public static void downloadFile(TpcCredentialsDTO dto) { logger.trace("Entering downloadFile() method"); Session session = null; Channel channel = null; ChannelSftp channelSftp = null; boolean success = false; try { JSch jsch = new JSch(); session = jsch.getSession(dto.getUsername(), dto.getHost(), dto.getPort()); session.setPassword(dto.getPassword()); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); logger

JSchException: Algorithm negotiation fail问题解决之路

我怕爱的太早我们不能终老 提交于 2019-11-29 22:07:17
最近一个需求用到了SFTP上传功能,同事之前已经封装好了SFTP工具类,用的是JSch,本着不要重复造轮子的想法,就直接拿来用了。交代下环境,JDK为1.7,JSch版本为0.1.51。自测通过、测试环境也OK,但上到生产环境却抛出Algorithm negotiation fail异常,当即傻眼,下面是具体的异常信息: com.jcraft.jsch.JSchException: Algorithm negotiation fail at com.jcraft.jsch.Session.receive_kexinit(Session.java:583) ~[jsch-0.1.51.jar:na] at com.jcraft.jsch.Session.connect(Session.java:320) ~[jsch-0.1.51.jar:na] 更纳闷的是,在本端(Client端)下用Linux命令SFTP(参考 linux下FTP、SFTP命令详解 )连接对端(Server端)是成功的,也能上传,首先排除了用户名/密码错误、网络不通、未添加白名单之类的原因了。然后想到是程序本身的问题,但为什么开发环境、测试环境都没抛错呢?难道是环境问题吗?用ssh -V命令查看ssh版本,测试环境的本端和对端,以及生产环境的本端均是“OpenSSH_5.3p1, OpenSSL 1.0.0