Java使用SSH远程访问Windows并执行命令

转载至:http://blog.csdn.net/carolzhang8406/article/details/6760430

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作为系统服务启动,回答是之后就安装完成了。

安装完成之后,双击freesshd图标(桌面或开始菜单),不会直接打开配置界面,而是需要在任务栏的“显示隐藏图标”(正三角图标)处右键freessh图标,选择setting。

配置用户名和密码:(java代码中连接的用户名和密码)

检查freesshd server status状态。切换至“server status”标签页,查看“SSH server is running”是否打钩,如果没有,点击下方连接检查,如果报错“the specified address is already in use”,则是由于安装时询问“Do you want to run FreeSSHd as a system service?”选择了“是”导致的,只需要在services.msc中将该服务停掉,再在配置界面启动,显示为打钩状态即可。

java代码如下:

[java] view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5.   
  6. import ch.ethz.ssh2.Connection;  
  7. import ch.ethz.ssh2.Session;  
  8. import ch.ethz.ssh2.StreamGobbler;  
  9.   
  10. public class SSHWindows {  
  11.   
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.          String hostname ="192.168.30.10";  
  15.             String username="administrator";  
  16.             String password="Talent123";  
  17.             try{  
  18.                 //建立连接  
  19.                 Connection conn= new Connection(hostname);  
  20.            //     System.out.println("set up connections");  
  21.                 conn.connect();  
  22.                 //利用用户名和密码进行授权  
  23.                 boolean isAuthenticated = conn.authenticateWithPassword(username, password);  
  24.                 if(isAuthenticated ==false)  
  25.                 {  
  26.            //       System.out.println("--------");  
  27.                     throw new IOException("Authorication failed");  
  28.                 }  
  29.                 //打开会话  
  30.                 Session sess = conn.openSession();  
  31.             //    System.out.println("cmd----");  
  32.                 //执行命令  
  33.                 sess.execCommand("ruby C:\WhatWeb-master\whatweb --output-xml http://216.139.147.75:443/");  
  34.            //     System.out.println("The execute command output is:");  
  35.                 InputStream stdout = new StreamGobbler(sess.getStdout());  
  36.                 BufferedReader br = new BufferedReader(new InputStreamReader(stdout));  
  37.                 while(true)  
  38.                 {  
  39.                     String line = br.readLine();  
  40.                     if(line==null) break;  
  41.                     System.out.println(line);  
  42.                 }  
  43.              //   System.out.println("Exit code "+sess.getExitStatus());  
  44.                 sess.close();  
  45.                 conn.close();  
  46.            //     System.out.println("Connection closed");  
  47.                   
  48.             }catch(IOException e)  
  49.             {  
  50.                 System.out.println("can not access the remote machine");  
  51.             }  
  52.         }  
  53.   
  54.   
  55. }  



以上代码依赖于ssh2的jar包。下载地址:http://www.ganymed.ethz.ch/ssh2/

注意: 该服务端需安装在数据库所在服务器

原文地址:https://www.cnblogs.com/fuhengheng/p/8043647.html