删除远程服务器上的目录
同之前的说明,先在工具类中添加一个删除远程目录的方法
- /**
- * 删除远程服务器上的目录
- * @param host 主机ip
- * @param username 登录用户名
- * @param password 登录密码
- * @param remoteDerectory 删除的目录路径 eg:/usr/local/test
- * @param port ssh端口
- * @throws IOException 操作异常
- */
- public static void rmDirectory(String host, String username,
- String password,
- String remoteDerectory, int port) throws IOException {
- if (logger.isInfoEnabled()) {
- logger.info("rm [" + remoteDerectory + "] is " + host
- + remoteDerectory);
- }
- Connection conn = null;
- try {
- conn = getOpenedConnection(host, username, password, port);
- SFTPv3Client sftpClient = new SFTPv3Client(conn);
- sftpClient.rmdir(remoteDerectory);sftpClient.close();
- } finally {
- if( null != conn ) {
- conn.close();
- }
- }
- }
服务器上新建一个test目录:
- public static void main(String[] args) {
- String remoteDir = "/usr/local/test/";
- try {
- CommandRunner.rmDirectory("172.16.39.141", "root", "123456", remoteDir,
- 22);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
执行结果: