-1

I'm trying to write to write to a named pipe over a connection facilitated by jsch.

        // connect to server
        JSch ssh = new JSch();
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
            System.out.println("Connection established.");
        System.out.println("Crating SFTP Channel.");
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
        System.out.println("SFTP Channel created.");

        // write to pipe
        OutputStream strm = sftpChannel.put(remoteFile);

        // failed attempts
        // BufferedWriter wrtr = new BufferedWriter(new PrintWriter(new OutputStreamWriter(strm)));
        // PrintWriter wrtr = new PrintWriter(new BufferedOutputStream(strm));

        // Current version
        BufferedWriter wrtr = new BufferedWriter(new PrintWriter(strm));

        wrtr.write("hello world");
        wrtr.flush();

        session.disconnect();
        sftpChannel.disconnect();
        wrtr.close();

The connect to server part is essentially an exact copy from: SSH connection with Java

The code will even wait for something to be reading the pipe on the other side, meaning that if I don't use:

cat pipe

It will wait till I do so, and then once I have it will print out nothing and the cat pipe call will be over. Essentially it appears that I'm writing "" to the pipe instead of "hello world"

Any help would be much appreciated, thanks.

Community
  • 1
  • 1
  • Did you first try to do the same using some command-line SFTP client? It can be the SFTP server that causes the troubles, not your code. – Martin Prikryl Mar 27 '15 at 07:04
  • Yeah I tried that, writing to a named pipe with java on the server is fine. – Colan Biemer Mar 27 '15 at 16:31
  • No, I mean if you have tried to write to the named pipe over SFTP (remotelly) with SFTP client. – Martin Prikryl Mar 27 '15 at 17:41
  • @ColanBiemer You realize that your java program isn't really writing to the named pipe, right? The sftp server program on the remote host is what actually opens and writes to the named pipe. What SFTP server program is this? Have you confirmed it supports writing to named pipes? – Kenster Mar 27 '15 at 19:16

1 Answers1

0

I suspect that if you talked to any SFTP server software author, he'd tell you that interacting with named pipes through SFTP isn't supported, and that you're on your own. Having said that, you could try requesting append mode for the put operation:

OutputStream strm = sftpChannel.put(remoteFile, ChannelSftp.APPEND);
                                                ^^^^^^^^^^^^^^^^^^
BufferedWriter wrtr = new BufferedWriter(new PrintWriter(strm));
wrtr.write("hello world");

It might work, or it might not. It depends on the SFTP server.

At the protocol level, an SFTP write request specifies what file to write to, where in the file to write it, and what data to write. Based on the the OpenSSH 5.8 source code that I have at hand, that version of the SFTP server program will always seek to the correct location in the file before writing data to it. Pipes don't support seeking, so the sftp-server program would report a seek failure to the client without trying to write the data.

However, the OpenSSH 6.6 version of sftp-server will omit the seek if the file was opened in append mode. I haven't tested it, but it may be possible to write to a named pipe using that version of the server, if the client requests append mode for the file in the first place. And by extension, it may work for some other versions of the OpenSSH server as well.

Kenster
  • 23,465
  • 21
  • 80
  • 106