1

I want to make an alias for openconnect, the alias is

echo "password" | sudo openconnect server --user=username --passwd-on-stdin --no-cert-check

Everything works fine but my problem is that each time I need to enter my password because I'm using sudo. What can I do to also supply the password for sudo in this alias?

I have seen this question but it didn't help.

1 Answers1

3

You can use:

echo "<sudo password>" | sudo -S sh -c "echo \"<openconnect password\" | openconnect server --user=username --passwd-on-stdin --no-cert-check"

replacing with the password needed for root and with your openconnect password.

This should work because you first pipe in the sudo password to sudo itself (using -S as per your linked question, to take in the password via stdin) and then executing (as root) another command - this is what sh -c does (when followed by a string that is your command). We append our command to sh -c which includes piping in the openconnect password to openconnect - at this point, openconnect is working as root and so doesn't need sudo before it.

While I don't use openconnect, I tested with echo "mypassword" | sudo -S sh -c "whoami | xargs -i echo {} to test piping the value from whoami which tells me root, confirming the sudo works. You may get a line into stdout (your terminal) stating to enter in the password, but it will still work - you can ignore this line if you specified it in stdin.

QuickishFM
  • 1,082