我有从TelnetClient获得的OutputStream和InputStream对象,它们连接到UNIX服务器(使用Ubuntu和SunOS测试)。
提供登录,密码和接收命令提示后,我发送“cat> / tmp / qqq”命令,该命令应将其输入流复制到/ tmp / qqq文件。发送命令后,在输出流上执行.flush()。我发送cat命令的输入完成00,04字节作为EOF标记。但猫的执行不会停止。
如何正确发送EOF?
PS。 使用JSch库通过SSH工作时我也有同样的问题
这是一个很常见的例子,我用它进行测试:
import com.prov.ExpectReader
import org.apache.commons.net.telnet.TelnetClient
import java.util.regex.Pattern
TelnetClient telnetClient = new TelnetClient();
telnetClient.setConnectTimeout(60000);
telnetClient.connect("192.168.1.50", 23);
telnetClient.setSoTimeout(3000);
def os = telnetClient.getOutputStream()
def is = telnetClient.getInputStream()
def er = new ExpectReader(is)
er.read(new ExpectReader.PatternChecker(Pattern.compile("^.*login: \$", Pattern.DOTALL), 0), 3000, 3000, 3000)
os.write("LoGiN\n".getBytes())
os.flush()
er.read(new ExpectReader.PatternChecker(Pattern.compile("^.*Password: \$", Pattern.DOTALL), 0), 3000, 3000, 3000);
os.write("PaSsWoRd\n".getBytes())
os.flush()
er.read(new ExpectReader.PatternChecker(Pattern.compile(".*\\\$ \$", Pattern.MULTILINE | Pattern.DOTALL), 0), 3000, 3000, 3000)
os.write("cat > /tmp/qqq\n".getBytes())
os.flush()
os.write("One two three".getBytes())
os.flush()
byte[] eof = [00, 04]
os.write(eof)
os.flush()
println er.read(new ExpectReader.PatternChecker(Pattern.compile(".*\\\$ \$", Pattern.MULTILINE | Pattern.DOTALL), 0), 3000, 3000, 3000)
最后期望读者执行失败,超时异常说:
com.prov.IOTimeoutException: Timeout 3000 exceeded, bytes that have been read: 'cat > /tmp/qqq
One two three^@'
at com.prov.ExpectReader.read(ExpectReader.java:88)
at com.prov.ExpectReader$read.call(Unknown Source)
at com.prov.customization.ConnectionIter.run(ConnectionIter.groovy:51)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)