2

I'm currently putting together a quick script that deploys multiple vagrant machines locally for dev purposes. Par of the procedure consists of adding the ssh key to the vagrant boxes.

So far, getting the path of the ssh keys is fairly easy: vagrant ssh-config | grep IdentityFile | awk '{print $2}'

It gives the expected output:

/Users/admin/vms/.vagrant/machines/deploy-node.vagrant/virtualbox/private_key /Users/admin/vms/.vagrant/machines/backend-node.vagrant/virtualbox/private_key /Users/admin/vms/.vagrant/machines/app-node.vagrant/virtualbox/private_key

However, piping the result to ssh-add throws an exception:

ssh_askpass: exec(/usr/X11R6/bin/ssh-askpass): No such file or directory

When I manually type the commands one by one, the ssh-add works OK: ssh-add /Users/admin/vms/.vagrant/machines/deploy-node.vagrant/virtualbox/private_key

Identity added...

Therefore, I believe the issue comes from the result of awk that returns a block of 3 lines instead of 3 times a line to the pipe.

This suspicion can be confirmed by issuing: vagrant ssh-config | grep IdentityFile | awk '{print $2}' | wc -l that returns 3 when I would expect to get 3 times the 1 value.

would someone know how to split the result of the awk so the ssh-add works properly?

E. Jaep
  • 133
  • try awk '{print $2"\n"}' and let me know if this works. – Videonauth Oct 05 '18 at 06:55
  • @Videonauth it works. Thanks. If you put your answer as 'answer' instead of comment, I'll accept it. Unfortunately, it does not solve my initial problem, but now I believe it is related to the way ssh-add handles piping – E. Jaep Oct 05 '18 at 06:58
  • Well I'll write it now, until now I was just guessing and thus only commented :) – Videonauth Oct 05 '18 at 06:59

1 Answers1

1

You can make it work with ordering awk to add a newline-character (\n) to each line:

vagrant ssh-config | awk '/IdentityFile/{print $2"\n"}'

And then pipe this to ssh-add.

Videonauth
  • 33,845