Jsch Shell 不接受来自终端的输入
Jsch Shell won't accept input from Terminal
我写了一个简单的 groovy 程序,它使用 Jsch 库建立 ssh 隧道并在目标服务器上打开 shell。脚本连接正常,shell 打开。在 IntelliJ 中,如果我 运行 程序,我可以将输入输入 shell 并获得后续输出。但是,如果我尝试在终端或 cmd 中执行相同的操作,它连接正常,但我无法输入任何输入,因此无法 运行 命令。
println "Opening connection to ${sshUser}@${sshHost}:${sshPort}"
Properties config = new Properties()
config.put("StrictHostKeyChecking", "no")
JSch jsch = new JSch()
Session sshSession = jsch.getSession(sshUser, sshHost, sshPort as int)
sshSession.setPassword(sshPass)
sshSession.setConfig(config)
sshSession.connect()
println "Connected"
println "Forwarding connection to ${targetHost}:${targetPort}"
def assignedPort = sshSession.setPortForwardingL(0, targetHost, targetPort as int)
println "Got port $assignedPort"
// Set the session to open as a Shell
Channel channel = targetSession.openChannel("shell")
// Set Input and Output streams
channel.setInputStream(System.in)
channel.setOutputStream(System.out);
// Connect
channel.connect()
更新
进一步阅读后,似乎要使此工作正常进行 Windows 提示有一个 hack。然而,JCraft 提供的代码有许多我正在努力解决的问题:
channel.setInputStream(new FilterInputStream(System.in){
public int read(byte[] b, int off, int len)throws IOException{
return in.read(b, off, (len>1024?1024:len));
}
});
我得到了非常有用的错误 "Wrong Statement"
我最终找到了一个非常简单的解决方案。 Windows 提示 hack 确实有效,但需要进行如下调整:
return System.in.read(b, off, (len>1024?1024:len));
谢谢大家的帮助
我写了一个简单的 groovy 程序,它使用 Jsch 库建立 ssh 隧道并在目标服务器上打开 shell。脚本连接正常,shell 打开。在 IntelliJ 中,如果我 运行 程序,我可以将输入输入 shell 并获得后续输出。但是,如果我尝试在终端或 cmd 中执行相同的操作,它连接正常,但我无法输入任何输入,因此无法 运行 命令。
println "Opening connection to ${sshUser}@${sshHost}:${sshPort}"
Properties config = new Properties()
config.put("StrictHostKeyChecking", "no")
JSch jsch = new JSch()
Session sshSession = jsch.getSession(sshUser, sshHost, sshPort as int)
sshSession.setPassword(sshPass)
sshSession.setConfig(config)
sshSession.connect()
println "Connected"
println "Forwarding connection to ${targetHost}:${targetPort}"
def assignedPort = sshSession.setPortForwardingL(0, targetHost, targetPort as int)
println "Got port $assignedPort"
// Set the session to open as a Shell
Channel channel = targetSession.openChannel("shell")
// Set Input and Output streams
channel.setInputStream(System.in)
channel.setOutputStream(System.out);
// Connect
channel.connect()
更新
进一步阅读后,似乎要使此工作正常进行 Windows 提示有一个 hack。然而,JCraft 提供的代码有许多我正在努力解决的问题:
channel.setInputStream(new FilterInputStream(System.in){
public int read(byte[] b, int off, int len)throws IOException{
return in.read(b, off, (len>1024?1024:len));
}
});
我得到了非常有用的错误 "Wrong Statement"
我最终找到了一个非常简单的解决方案。 Windows 提示 hack 确实有效,但需要进行如下调整:
return System.in.read(b, off, (len>1024?1024:len));
谢谢大家的帮助