使用Java JSch执行命令时,可以通过将密码中的特殊字符进行转义或者使用单引号来避免特殊字符的问题。

以下是一个示例代码:

import com.jcraft.jsch.*;

public class SSHCommandExecutor {
    public static void main(String[] args) {
        String host = "192.168.35.203";
        int port = 22;
        String username = "ly_cloud_xtgl";
        String password = "ly$_cloud_xtgl";
        String command = "exp ly_cloud_xtgl/ly\\$_cloud_xtgl@192.168.35.203:1521/orcl file=/tmp/backup/1691633904031.dmp log=/tmp/backup/1691633904031.log statistics=none";

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            InputStream in = channel.getInputStream();
            channel.connect();

            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    System.out.print(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    System.out.println("exit-status: " + channel.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }

            channel.disconnect();
            session.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上述代码中,我们使用JSch库来连接SSH服务器,并执行命令。将密码中的$符号进行转义,即用\\$代替$。然后将整个命令字符串传递给setCommand方法来执行。

请注意,这只是一个简单的示例代码,您需要根据您的具体需求进行修改和扩展

exp ly_cloud_xtglly$_cloud_xtgl192168352031521orcl file=tmpbackup1691633904031dmp log=tmpbackup1691633904031log statistics=none这个oracle exp备份导出命令密码存在$特殊符号怎么使用java JSch执行

原文地址: https://www.cveoy.top/t/topic/izn8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录