com.jcraft.jsch.JSchException: PortForwardingL: 本地端口 127.0.0.1:6969 已经注册

com.jcraft.jsch.JSchException: PortForwardingL: local port 127.0.0.1:6969 is already registered

我有一个连接到名为 Bastion 的服务器的程序。然后我将 PortForward 转发到只能通过 Bastion 访问的另一台服务器 (BE)。我有这个工作,我可以在 BE 上执行 shell 命令,但是我经常在每个 运行.

结束时得到这个
> com.jcraft.jsch.JSchException: PortForwardingL: local port
> 127.0.0.1:6969 is already registered.

这是我的代码:

public static void  connect2BE() throws JSchException, IOException, SftpException {

        int assinged_port = s.setPortForwardingL(6969, "be22", 22);

        innerSession = js.getSession("user", "127.0.0.1", assinged_port);
        innerSession.setPassword("pw");
        Properties innerConfig = new Properties();
        innerConfig.put("StrictHostKeyChecking", "no");
        innerSession.setConfig(innerConfig);
        innerSession.connect();
        System.out.println("BackEnd session created.");
        createPackagesList();
        innerSession.disconnect();
    }

    public static void createPackagesList() throws JSchException, IOException, SftpException {

        Channel innerChannel = innerSession.openChannel("shell");
        OutputStream out = innerChannel.getOutputStream();
        PrintStream ps = new PrintStream(out, true);

        innerChannel.connect();
        ps.println("rm be22-rpm.txt");
        ps.println("rm be22-sort.txt");
        ps.println("rpm -qa | wc -l");
        ps.println("rpm -qa > be22-rpm.txt");
        ps.println("cat be22-rpm.txt | sort > be22-sort.txt");
        //ps.println("scp be22-sort user@be74:/Package_Checker");
        //ps.println("yes");
        //ps.println("pw");
        ps.close();
        InputStream in=innerChannel.getInputStream();
        byte[] bt=new byte[1024];
        while(true) {
            while (in.available() > 0) {
                int i = in.read(bt, 0, 1024);
                if (i < 0)
                    break;
                String str = new String(bt, 0, i);
                //displays the output of the command executed.
                System.out.print(str);


            }

            if(innerChannel.isClosed())
            {

                break;
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            innerChannel.disconnect();
            innerSession.disconnect();
        }
        //downloadList(innerSession);

    }

这是我运行上面的输出:

[emctiernan@be22 ~]$ rm be22-rpm.txt
[emctiernan@be22 ~]$ 
[emctiernan@be22 ~]$ rm be22-sort.txt
[emctiernan@be22 ~]$ 
[emctiernan@be22 ~]$ rpm -qa | wc -l
489


[emctiernan@be22 ~]$ rpm -qa > be22-rpm.txt
[emctiernan@be22 ~]$
[emctiernan@be22 ~]$ cat be22-rpm.txt | sort > be22-sort.txt
[emctiernan@be22 ~]$ 
[emctiernan@be22 ~]$ com.jcraft.jsch.JSchException: PortForwardingL: local port 127.0.0.1:6969 is already registered.

Process finished with exit code 0

在此先感谢您的帮助

我真的弄清楚了它的问题所在。结果我打电话给 connect2BE 两次。这意味着我在连接后尝试连接它。我犯了一个非常愚蠢的错误。