Provide password to `nmcli con up` in terminal 14.04

I am trying to connect to my VPN using terminal. My VPN password is a TOTP, so cannot save it to the connection conf file. When I try to connect, a dialog appear to enter password. I want to do it in terminal itself as I can generate my TOTP from a script. I just couldn't figure out any way. Is there a way to specify password in command line or send password from command line to the dialog?


Solution 1:

You can write-update a password to a seperate file. This works for me:

echo "vpn.secrets.password:MY_PASSWORD" > /my/vpn/password
nmcli con up uuid MY_UUID passwd-file /my/vpn/password

Solution 2:

In case you need to create the password with a script or similar and then pass it to nmcli without storing it on the disk, the following works for me (nmcli 1.30.0) and might be possible to be adapted:

echo vpn.secrets.password:$PW | /usr/bin/nmcli c up $VPN_CON_NAME passwd-file /dev/fd/0

It unfortunately does not support the standard linux - for standard input...

Solution 3:

You can set the password using the following command:

nmcli con mod VPNID vpn.secrets "password=VPNPASS"

where VPNPASS is the VPN password.

In order for this to work, you have to enable the pasword storage option in NetworkManager, as shown in this picture:

[1]: https://i.stack.imgur.com/3eU8g.png

I am using Ubuntu 16.04.