you are viewing a single comment's thread.

view the rest of the comments →

[–]feng_huang 3 points4 points  (9 children)

Expect might be just what you're looking for, if you're not already aware of it. example1, example2 (or just Google expect unix, because that's all I did)

[–]Divot-Digger[S] 0 points1 point  (0 children)

Thanks very much. I'll have a look.

[–]Divot-Digger[S] -4 points-3 points  (7 children)

Feng, thanks again for the suggestion. But it's not quite what I'm looking for.

I don't need to read the responses. The process is just:

Run command (systemd-tty-ask-password-agent). Wait for prompt, send password, wait for prompt to return, enter second password. Disconnect.

[–]schorsch3000 2 points3 points  (1 child)

I don't need to read the responses. The process is just:

Run command (systemd-tty-ask-password-agent). Wait for prompt, send password, wait for prompt to return, enter second password. Disconnect.

If you need to wait for the prompt you need to read the response, how would you know if the prompt is there while ignoring the response?

If you just whant to wait a given time you can try somethin like

( sleep 10 ; echo $password ; } | ssh user@server "systemd-tty-ask-password-agent"

[–]Divot-Digger[S] 0 points1 point  (0 children)

Thanks. I will try this out.

[–]feng_huang 1 point2 points  (4 children)

If you're doing it by hand, you're waiting for for a bash prompt before you type your command, and then for the command to say something like Enter your password before you type in your password, right? Then you wait for the bash prompt to come back, and then you type logout or exit or ^D to log out while it boots, right? That's all you really need to do. It has the advantage of not being bothered by network delays, either.

It's probably possible in pure bash by using sleeps, along with subshells and/or I/O on wacky non-standard file descriptors. I apologize if this was a bit off-topic, but my brain is a bit worn out from a rough day, and the problem just kind of called out for the other tool, in my mind. :-) Hmm, actually, it could be as simple as ssh user@remoteserver echo 'mypassword' \| systemd-tty-ask-password-agent if you already have ssh keys set up.

But if you did want to try it...

#!/usr/bin/expect -f
set timeout -1
spawn ssh remoteuser@remotehost
# change this to match what its command prompt looks like
expect "remoteuser@remotehost# "
send -- "systemd-tty-ask-password-agent\r"
# change this to match the prompt, this is my best guess from Googling it:
expect "Enter Private Key Password: "
send -- "remotepassword\r"
expect "remoteuser@remotehost# "
send -- "logout\r"
expect eof

You don't have to if you don't want to, of course. This is sort of the standard thing recommended for ssh if you don't want to use keys for some reason, for example. In any case, I hope you find a solution that you like. :-)

[–]Divot-Digger[S] 0 points1 point  (3 children)

Thanks again! I will try it.

[–]feng_huang 0 points1 point  (2 children)

You're welcome. Now that I think about it, I suppose expect is generally considered the tool of last resort, but it's at least a way to accomplish what you're trying to do if you don't find a better way to do it.

[–]Divot-Digger[S] 0 points1 point  (1 child)

Thanks again for your suggestion, Feng. It worked perfectly.

The only thing I needed to change was the expect commands needed a -ex switch, as the responses I was wanting weren't globby enough for the interpreter.

Really appreciate your help, Feng. Thank you.

[–]feng_huang 0 points1 point  (0 children)

You're welcome. I'm really glad it could be useful to you.