1

I need to send some credentials defined in a file to a vpn connection command which is waiting for username and password to be entered when executed. There is a similar requirement to my question Automatically enter input in command line.

The following solution provided does work for me too:

printf 'username\npassword\n' | /usr/sbin/openconnect -i vpn0 ...

(executed inside a shell script). But I need to pass the credentials contained in a file (env_properties) and loaded as environment variables. I'm loading the file content using the following command:

[ -f env_properties] && . env_properties

The content looks as follows

export VPN_USERNAME=myUsername
export VPN_PASSWORD=myPassword

However, such a construct doesn't work anymore:

printf "${VPN_USERNAME}\n${VPN_PASSWORD}\n" | /usr/sbin/openconnect -i vpn0 ...

It seems that the input isn't recognized correctly for whatever reason. And neither this command

printf "/usr/sbin/openconnect -i vpn0 ... < env_properties

with a modified file content containing only plain username and password, each in a new line. Same applies to the here document solution.

Any ideas?

Byte Commander
  • 107,489
user35934
  • 113

2 Answers2

1

Instead of:

printf "${VPN_USERNAME}\n${VPN_PASSWORD}\n"

try this:

printf "%s\n%s\n" “$VPN_USERNAME” “$VPN_PASSWORD”
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94
  • thx for your reply, but it didn't work out either. It seems like the variable expansion somehow distorts the input strings or so – user35934 Jun 18 '17 at 17:46
0

Silly me! The input file has carriage returns which the Linux system of course cannot deal with. After removing them the print command I've posted now works.

user35934
  • 113