簡體   English   中英

切換用戶后如何使用JSch庫獲取SFTP服務器目錄中所有文件的列表?

[英]How to get list of all files within a directory on SFTP server using JSch library after switching a user?

我編寫了一個程序,在切換到特定用戶后,使用 JSch 庫讀取目錄中的所有文件。

但問題是程序從原始用戶的 / 目錄而不是su用戶的 ( /apps/my-app/ ) 目錄讀取並顯示所有文件。

總而言之,該程序執行以下操作:

  1. 打開session
  2. 打開exec通道
  3. 執行su命令並cd進入我們要列出其文件的目錄
  4. 打開sftp通道
  5. 列出當前目錄中的文件,該目錄應該是我們之前cd進入的目錄
  6. 關閉sftp通道
  7. 關閉exec通道
  8. 關閉session

SuListFilesMain.java

public class SuListFilesMain {

    static String cmd = "echo " + DecryptionUtil.decrypt(Constants.userPasswd) + " | su jackm -c \"cd /apps/my-app/ ; pwd\"";

    public static void main(String[] args) throws JSchException, SftpException, Exception {
        //open session
        Session session = SessionUtil.openSession();

        //open exec channel, switch to su and execute command cd
        Channel execChannel = ChannelUtil.openChannel(session, cmd);
        //capture cmd output
        InputStream output = execChannel.getInputStream();
        String result = CharStreams.toString(new InputStreamReader(output));
        //CORRECT: PRINTS /APPS/MY-APP (See Output below)
        System.out.println(result); 

        //open sftp channel
        ChannelSftp sftpChannel = ChannelUtil.openSftpChannel(session);
        //ls
        List<String> allFiles = new ArrayList<>();
        Vector<LsEntry> vector = sftpChannel.ls(".");
        for (LsEntry entry : vector) {
            allFiles.add(entry.getFilename());
        }
        //INCORRECT: PRINTS FROM / DIRECTORY INSTEAD OF /APPS/MY-APP (See Output below)
        System.out.println(allFiles); 
        //close sftp channel
        sftpChannel.exit();

        //close exec channel
        ChannelUtil.closeChannel(execChannel);

        //close session
        SessionUtil.closeSession(session);
    }

}

SessionUtil.java

public class SessionUtil {

    private static final Properties envProps = ReadPropertyUtil.readAllProperties("application.properties");

    public static Session openSession() throws JSchException {
        JSch jSch = new JSch();
        String host = envProps.getProperty("host");
        String username = envProps.getProperty("username");
        String pwd = DecryptionUtil.decrypt(envProps.getProperty("pwd.encrypted"));
        Session session = jSch.getSession(username, host, 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(pwd);
        System.out.println("Connecting SSH to " + host + " - Please wait for few seconds... ");
        session.connect();
        System.out.println("Connected!\n");
        return session;
    }

    public static void closeSession(Session session) {
        if (null != session) {
            session.disconnect();
            System.out.println("\nDisconnected channel and session");
        }
    }

}

ChannelUtil.java

public class ChannelUtil {

    public static Channel openChannel(Session session, String cmd) throws JSchException {
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(cmd);
        channel.connect();
        return channel;
    }

    public static ChannelSftp openSftpChannel(Session session) throws JSchException {
        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        return sftpChannel;
    }

    public static void closeChannel(Channel channel) {
        System.out.println("Closing channel...");
        if (null != channel) {
            channel.setInputStream(null);
            channel.disconnect();
        }
    }

}

Output:

Connected!

/apps/my-app

[local, lib64, ., nfs, etc, boot, srv, home, proc, sys, media, lib, bin, run]
Closing channel...

Disconnected channel and session

SSH 通道完全獨立。 在一個通道上執行su對其他通道沒有任何影響。

您必須在 SFTP 通道上執行su ,這是一項相當復雜的任務,部分包含在我對以下問題的回答中:
當必須切換用戶時使用 JSch 到 SFTP

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM