ssh上传下载

214 阅读1分钟
pom引入
     <dependency>
        <groupId>ch.ethz.ganymed</groupId>
        <artifactId>ganymed-ssh2</artifactId>
        <version>build210</version>
    </dependency>

public class SSHUtil {

private static Logger logger = LoggerFactory.getLogger(SSHUtil.class);

public static Connection getConnect(String user,String password ,String ip,int port ) {
    Connection conn=new Connection(ip, port);
    try {
        conn.connect();
        conn.authenticateWithPassword(user, password);
    }catch(Exception e) {
        logger.error(e.getMessage(), e);
    }
    return conn;
}

public static String exeCmd(String cmd,String user,String password ,String ip,int port){
    StringBuffer buffer = new StringBuffer();
    Connection connection = null;
    Session session = null;
    try {
        connection=getConnect(user, password, ip, port);
        session=connection.openSession();
        session.execCommand(cmd);
        InputStream in = new StreamGobbler(session.getStdout());
        BufferedReader brs = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        String line = null;
        while ((line = brs.readLine()) != null) {
            buffer.append(line + "\n");
        }
        if(buffer.length()==0){
            InputStream errIn = new StreamGobbler(session.getStderr());
            BufferedReader errBrs = new BufferedReader(new InputStreamReader(errIn,"UTF-8"));
            String errLine = null;
            while ((errLine = errBrs.readLine()) != null) {
                buffer.append(errLine + "\n");
            }
        }
    }catch(Exception e) {
        logger.error(e.getMessage(), e);
    }finally {
        try {
            session.close();
        }catch(Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            connection.close();
        }
    }
    return buffer.toString();
}

public static boolean uploadFile(String remoteFilePath,String localFilePath,String user,String password ,String ip,int port) {
    boolean bool=false;
    Connection connection=null;
    try {
        connection=getConnect(user, password, ip, port);
        SCPClient scpClient = connection.createSCPClient();
        scpClient.put(localFilePath, remoteFilePath);
        bool=true;
    }catch(IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        bool =false;
    }finally {
        connection.close();
    }
    return bool;
}

public static boolean uploadFile(String remoteFilePath,String localFilePath,String mode,String user,String password ,String ip,int port) {
    boolean bool=false;
    Connection connection=null;
    try {
        connection=getConnect(user, password, ip, port);
        SCPClient scpClient = connection.createSCPClient();
        scpClient.put(localFilePath, remoteFilePath,mode);
        bool=true;
    }catch(IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        bool =false;
    }finally {
        connection.close();
    }
    return bool;
}

public static boolean downloadFile(String remoteFilePath,String localFilePath,String user,String password ,String ip,int port) {
    boolean bool=false;
    Connection connection=null;
    try {
        connection=getConnect(user, password, ip, port);
        SCPClient scpClient = connection.createSCPClient();
        scpClient.get(remoteFilePath, localFilePath);
        bool=true;
    }catch(IOException ioe) {
        logger.error(ioe.getMessage(),ioe);
        bool =false;
    }finally {
        connection.close();
    }
    return bool;
}

public static boolean mkDir(String dirName, int posixPermissions,String user,String password ,String ip,int port) {
    boolean bool=false;
    Connection connection=null;
    SFTPv3Client sftPv3Client=null;
    try {
        connection=getConnect(user, password, ip, port);
        sftPv3Client = new SFTPv3Client(connection);
        sftPv3Client.mkdir(dirName, posixPermissions);
        bool=true;
    }catch(IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        bool =false;
    }finally {
        try {
            sftPv3Client.close();
        }catch(Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            connection.close();
        }
    }
    return bool;
}

public static boolean rmDir(String dirName,String user,String password ,String ip,int port) {
    boolean bool=false;
    Connection connection=null;
    SFTPv3Client sftPv3Client=null;
    try {
        connection=getConnect(user, password, ip, port);
        sftPv3Client = new SFTPv3Client(connection);
        sftPv3Client.rmdir(dirName);
        bool=true;
    }catch(IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        bool =false;
    }finally {
        try {
            sftPv3Client.close();
        }catch(Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            connection.close();
        }
    }
    return bool;
}

public static boolean rmFile(String fileName,String user,String password ,String ip,int port) {
    boolean bool=false;
    Connection connection=null;
    SFTPv3Client sftPv3Client=null;
    try {
        connection=getConnect(user, password, ip, port);
        sftPv3Client = new SFTPv3Client(connection);
        sftPv3Client.rm(fileName);
        sftPv3Client.close();
        bool=true;
    }catch(IOException ioe) {
        logger.error(ioe.getMessage(), ioe);
        bool =false;
    }finally {
        try {
            sftPv3Client.close();
        }catch(Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            connection.close();
        }
    }
    return bool;
}

}