pom.xml添加的依赖:
1 2 3 4 5 6 |
<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 --> <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>262</version> </dependency> |
这里注意,不同版本的这玩意用法有差别。这里我使用的是262。
操作类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
import ch.ethz.ssh2.*; import java.io.*; /** * @Author: 风萧古道的博客 windypath.com * @Date: 2019/11/29 15:14 */ public class SSHUtil { // uploadFile to Linux /** * 上传文件到Linux * * @param ip ip地址 * @param username 登录用户名 * @param password 密码 * @param remoteFilePath 目标文件所在完整目录 * @param file 本地文件File对象 * @return */ public static boolean uploadFile(String ip, String username, String password, String remoteFilePath, File file) { FileInputStream input = null; BufferedOutputStream boutput = null; Connection conn = null; try { conn = new Connection(ip); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed."); } SCPClient scpClient = conn.createSCPClient(); boutput = scpClient.put(file.getName(), file.length(), remoteFilePath, "0600"); byte[] buffer = new byte[1024 * 8]; input = new FileInputStream(file); int length = -1; while ((length = input.read(buffer)) != -1) { boutput.write(buffer, 0, length); } boutput.flush(); } catch (IOException e) { e.printStackTrace(System.err); } finally { try { boutput.close(); input.close(); conn.close(); } catch (IOException e) { e.printStackTrace(System.err); } } return true; } /** * 从远程服务器上下载文件 * * @param ip ip地址 * @param username 用户名 * @param password 密码 * @param remoteFile 要下载的文件的完整路径 * @param localFileName 下载到本地的文件名 * @return */ public static boolean downloadFile(String ip, String username, String password, String remoteFile, String localFileName) { SCPInputStream binput = null; FileOutputStream output = null; BufferedOutputStream boutput = null; Connection conn = null; try { File file = new File(localFileName); conn = new Connection(ip); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed."); } SCPClient scpClient = conn.createSCPClient(); binput = scpClient.get(remoteFile); output = new FileOutputStream(file); boutput = new BufferedOutputStream(output); byte[] buffer = new byte[1024 * 8]; int length = -1; while ((length = binput.read(buffer)) != -1) { boutput.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(System.err); } finally { try { binput.close(); boutput.close(); output.close(); conn.close(); } catch (IOException e) { e.printStackTrace(System.err); } } return true; } /** * 获取远程文件的输入流 * * @param ip ip * @param username 用户名 * @param password 密码 * @param remoteFile 远程文件的完整地址 * @return 输入流 */ public static SCPInputStream getRemoteFileInputStream(String ip, String username, String password, String remoteFile) { SCPInputStream binput = null; Connection conn = null; try { conn = new Connection(ip); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed."); } SCPClient scpClient = conn.createSCPClient(); binput = scpClient.get(remoteFile); } catch (IOException e) { e.printStackTrace(System.err); } return binput; } /** * 执行远程服务器的控制台命令 * * @param ip ip地址 * @param username 用户名 * @param password 密码 * @param cmd 控制台命令 */ public static void executeCmd(String ip, String username, String password, String cmd) { Connection conn = null; try { conn = new Connection(ip); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed."); } Session sess = conn.openSession(); sess.execCommand(cmd + "\n"); 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); } } catch (IOException e) { e.printStackTrace(System.err); } finally { conn.close(); } } } |
值得一提的是,uploadFile()方法的最后一个参数传入的是指向本地文件的一个File,也就是要上传的文件,而在downloadFile()方法中,最后一个参数是字符串,就是将目标文件下载下来之后,要叫什么名字,记得带上后缀。
执行命令的话,执行一次就新开一次session。一个session(好像貌似)不能做两件事(如上传文件的过程中创建一个新文件夹,再以新文件夹的目录为目标上传目录)。