Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 切换用户后,如何使用JSch库获取SFTP服务器上目录中所有文件的列表?_Java_Ssh_Sftp_Jsch_Su - Fatal编程技术网

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

Java 切换用户后,如何使用JSch库获取SFTP服务器上目录中所有文件的列表?,java,ssh,sftp,jsch,su,Java,Ssh,Sftp,Jsch,Su,我编写了一个程序,在切换到特定用户后,使用JSch库读取目录中的所有文件 但问题是程序读取并显示原始用户/目录中的所有文件,而不是su用户(/apps/my app/)目录中的所有文件 总而言之,该计划做了以下工作: 打开会话 打开exec频道 执行su命令并将cd放入要列出其文件的目录 打开sftp频道 列出当前目录中的文件,该目录应该是我们先前cd插入的目录 关闭sftp频道 关闭exec频道 关闭会话 SuListFilesMain.java public class SuListFile

我编写了一个程序,在切换到特定用户后,使用JSch库读取目录中的所有文件

但问题是程序读取并显示原始用户/目录中的所有文件,而不是
su
用户(
/apps/my app/
)目录中的所有文件

总而言之,该计划做了以下工作:

  • 打开
    会话
  • 打开
    exec
    频道
  • 执行
    su
    命令并将
    cd
    放入要列出其文件的目录
  • 打开
    sftp
    频道
  • 列出当前目录中的文件,该目录应该是我们先前
    cd
    插入的目录
  • 关闭
    sftp
    频道
  • 关闭
    exec
    频道
  • 关闭
    会话
  • 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);
        }
    
    }
    
    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");
            }
        }
    
    }
    
    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();
            }
        }
    
    }
    
    ChannelUtil.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);
        }
    
    }
    
    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");
            }
        }
    
    }
    
    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();
            }
        }
    
    }
    
    输出:

    已连接!
    /应用程序/我的应用程序
    [本地、lib64、、nfs等、引导、srv、主、进程、系统、媒体、lib、bin、运行]
    正在关闭频道。。。
    断开的通道和会话
    
    SSH通道是完全独立的。在一个通道上执行
    su
    ,对其他通道没有任何影响

    您必须在SFTP频道上执行
    su
    ,这是一项相当复杂的任务,部分内容已在我的回答中介绍:

    SSH通道是完全独立的。在一个通道上执行
    su
    ,对其他通道没有任何影响

    您必须在SFTP频道上执行
    su
    ,这是一项相当复杂的任务,部分内容已在我的回答中介绍: