Sending EOF via apache's TelnetClient to the "cat > somefile"

194 views Asked by At

I have OutputStream and InputStream objects obtained from TelnetClient which is connected to UNIX server (tested with Ubuntu and SunOS).

After supplying login, password and receiving command prompt, I am sending "cat > /tmp/qqq" command, which should copy its input stream to /tmp/qqq file. After I sent the command, executed .flush() on output stream. I am sending input for cat command finished with 00, 04 bytes as EOF mark. But cat execution does not stops.

How to send EOF correctly?

PS. When working over SSH with JSch library I have the same problem too

Here is groovy example, which I was using for testing:

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)

Last expect reader execution fails with timeout exception saying:

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)
0

There are 0 answers