3

I'm trying to bind a command to a key using .xbindkeysrc, namely this command retrieves a password from keepassxc, and echoes it as stdin to an openconnect (for VPN connection) command, and finally closes the terminal. The following does all of this, and it works:

psswd=`keepassxc-cli show -sa password path/to/MYKEEPASS.kdbx MY_ENTRY` ; echo $psswd | sudo openconnect --background --protocol=gp https://my.url --user=MYUSERNAME --passwd-on-stdin ; exit

However, this wouldn't work as a command in .xbindkeysrc, it needs to be executed in a terminal, which can be done by putting the previous code between "$()" as follows (again, this works), BUT without the "; exit" final bit (it does not work with it), therefore the terminal is not closed after running the full command.

gnome-terminal -e "$(psswd=`keepassxc-cli show -sa password path/to/MYKEEPASS.kdbx MY_ENTRY` ; echo $psswd | sudo openconnect --background --protocol=gp https://my.url --user=MYUSERNAME --passwd-on-stdin)"

Q1: How can I tell the terminal to close after executing the full command?

In addition, this simpler syntax (within "") is understood by .xbindkeysrc:

"gnome-terminal -e "sudo openconnect --protocol=gp https://my.url --user=MYUSERNAME""

but simply adding "" to the full command is not enough for .xbindkeysrc to run it when I press the bound key; this leads to my 2nd question:

Q2: How can I integrate the full command in .xbindkeysrc?

Thanks a lot


EDIT: after using Grisha Levit's answer:

"gnome-terminal -- sh -c 'pass=$(keepassxc-cli show -a password my_keepass_DB my_ENTRY) && printf "%s\n" "$pass" | sudo openconnect --background --protocol=gp https://my.url --user=my_NAME --passwd-on-stdin'"

there is an problem with vpnc-script, shown in lines 2,5-7:

Lines 2,5-7 are showing an error

The bit without the gnome-terminal -- does work:

sh -c 'pass=$(keepassxc-cli show -a password my_keepass_DB my_ENTRY) && printf "%s\n" "$pass" | sudo openconnect --background --protocol=gp https://my.url --user=my_NAME --passwd-on-stdin'

but this wouldn't work in .xbindkeysrc

Miguel
  • 356
  • 1
  • 15
  • 1
    What version of `gnome-terminal` is it? Mine gives the warning `Option “-e” is deprecated and might be removed in a later version of gnome-terminal.`. That said, do you know about `sh -c 'echo foo; echo bar | cat'`? You can probably use this kind of "inline" script in .xbindkeysrc – Fravadona Mar 28 '23 at 19:45
  • 1
    Why are you wrapping your command in `$(...)`? Do you really expect that long pipeline to output the name of a command to execute?? – tripleee Mar 29 '23 at 10:30
  • @tripleee I'm only wrapping the first part: pass=$(keepassxc-cli show -a password my_keepass_DB my_ENTRY) ... – Miguel Mar 29 '23 at 11:11
  • Your question shows the command line `gnome-terminal -e "$(psswd=\`keepassxc-cli show -sa password path/to/MYKEEPASS.kdbx MY_ENTRY\` ; echo $psswd | sudo openconnect --background --protocol=gp https://my.url --user=MYUSERNAME --passwd-on-stdin)"` – tripleee Mar 29 '23 at 13:18
  • @tripleee I am using the command suggested in the EDIT note at the end of my post – Miguel Mar 29 '23 at 13:51
  • 1
    Requiring a terminal for something which is essentially a GUI anyway seems roundabout. Can you use a dialog utility like `zenity` instead? – tripleee Mar 29 '23 at 14:03

1 Answers1

2
  • You should pass the command to run in gnome-terminal as positional parameters rather than using the -e option. (i.e. gnome-terminal -- cmd arg...)
  • You can't use shell syntax directly, it must go through e.g. sh -c.

So the command could be like:

gnome-terminal -- sh -c 'pass=$(keepassxc-cli show -a password DB ENTRY) && printf "%s\n" "$pass" | sudo openconnect --background --passwd-on-stdin …'

If that works, it should exit the terminal after starting the openconnect command so no exit needed.


There's nothing special about this being in .xbindkeysrc -- just wrap the command above in double quotes.

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
  • 1
    Surprisingly, the double quotes do not need escaping or any special treatment in the .xbinkeysrc file. I was originally going to suggest piping the password but then thought it's probably a good idea to capture it first and only continue if that part worked instead of piping an empty string in case the KeePass command failed – Grisha Levit Mar 29 '23 at 07:17
  • Thanks, it is running but just at the end it "exits abnormally", please check screenshot in my edit (sorry, I had to to a screenshot since it was happening too fast for being able to copy the text) – Miguel Mar 29 '23 at 11:12
  • 1
    That seems like an issue specific to `openconnect --background` running in a terminal that's closed -- probably better if you can narrow down a separate question for that without the xkeybindrc file. – Grisha Levit Mar 29 '23 at 17:36