[切换用户后如何使用JSch库获取SFTP服务器上目录中所有文件的列表?

问题描述 投票:1回答:1

我编写了一个程序,该程序使用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();
        }
    }

}

输出:

Connected!

/apps/my-app

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

Disconnected channel and session
java ssh sftp jsch su
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.