Thank you, that was very helpful!

For reference, I hacked something together like this for now:

ord() {
  printf '%d' "'$1"
}

echo Welcome
/bin/echo -n "Please input your age: "
age=""
while true; do
    read -s -n1 ch # -s: do not echo input character. -n 1: read only 1
character
    o=$(ord $ch)
    case $o in
        127) #backspace
            age="${age%?}"
            printf '\x08 \x08'
            ;;
        0) #enter key pressed
            break
            ;;
        3) #Ctrl-C
            break
            ;;
        4) #Ctrl-D
            break
            ;;
        *)
            age="$age$ch"
            ;;
    esac
done

Best regards,
Alexandre


On Tue, Apr 29, 2014 at 10:56 PM, Guillaume Nodet <[email protected]> wrote:

> I just tried your script using the following command:
>
> ssh localhost sh /Users/gnodet/work/tmp/myscript.sh
>
> and I don't see the prompt either, so it may be the read command bypassing
> the prompt when the input stream is not the console.
>
> You could try the following:
>
> #!/bin/sh
> echo Welcome
> echo -n "Please input your age: "
> read age
> echo You are $age years old
>
> For the backspace, I think it's the same problem and read bypasses readline
> if the input stream is not the console.
>
> One limitation of sshd is about pty allocation : we can't use things such
> as http://man7.org/linux/man-pages/man3/openpty.3.html so the only way to
> have real interactive sessions is to have the input stream handled in java
> using jline for example.  Unless you find a command that can be interactive
> without using a real pseudo-terminal.
>
>
>
>
> 2014-04-29 19:02 GMT+02:00 Alexandre Gattiker <[email protected]>:
>
> > Hello,
> >
> > Is it possible to run interactive shell scripts using Apache SSHD? My
> > attempt was not very successful.
> >
> > Java code:
> >
> > SshServer sshd = SshServer.setUpDefaultServer();
> > sshd.setPort(45121);
> > String hostKey = new
> > File(App.class.getResource("hostkey.pem").toURI()).getAbsolutePath();
> > sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKey));
> > sshd.setShellFactory(new ProcessShellFactory(new String[] { "sh",
> > "myscript.sh" }, EnumSet.of(TtyOptions.ONlCr, TtyOptions.ICrNl,
> > TtyOptions.Echo)));
> > List<NamedFactory<UserAuth>> userAuthFactories = new
> > ArrayList<NamedFactory<UserAuth>>();
> > userAuthFactories.add(new UserAuthPassword.Factory());
> > sshd.setUserAuthFactories(userAuthFactories);
> > sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
> >     public boolean authenticate(String username, String password,
> > ServerSession session) {
> >         return true;
> >     }
> > });
> > sshd.start();
> >
> >
> > myscript.sh:
> >
> > #!/bin/sh
> > echo Welcome
> > read -p "Please input your age: " age
> > echo You are $age years old
> >
> >
> > The output of an SSH connection is only "Welcome" (the prompt does not
> > appear). Then, I can input a value and the characters are echoed, but
> > backspace doesn't work.
> >
> > Thanks in advance,
> > Alexandre
> >
>

Reply via email to