Skip to content

Instantly share code, notes, and snippets.

@geedew
Last active July 22, 2017 15:16
Show Gist options
  • Save geedew/11289350 to your computer and use it in GitHub Desktop.
Save geedew/11289350 to your computer and use it in GitHub Desktop.
Copying files to a Vagrant VM from host
#!/bin/sh
# Change these settings to match what you are wanting to do
FILE=/File/To/Copy
SERVER=localhost
PATH=/Where/To/Put/File
OPTIONS=`vagrant ssh-config | awk -v ORS=' ' '{print "-o " $1 "=" $2}'`
scp ${OPTIONS} $FILE vagrant@$SERVER:$PATH
@geedew
Copy link
Author

geedew commented Apr 25, 2014

@guibog
Copy link

guibog commented Sep 10, 2014

Hi, I got a "Host directive not supported as a command-line option" with Vagrant 1.4.3, had to tweak a bit:

OPTIONS=`vagrant ssh-config | grep -v '^Host ' | awk -v ORS=' ' '{print "-o " $1 "=" $2}'`

@mykter
Copy link

mykter commented Nov 30, 2014

To remove a trailing empty "-o = ", prepend the awk string with NF to filter out the blank line:

OPTIONS=`vagrant ssh-config | grep -v '^Host ' | awk -v ORS=' ' 'NF{print "-o " $1 "=" $2}'`

As the hostname and user are specified via the ssh config options, they don't need to be specified again. To copy a file from the host to a Vagrant VM:

scp ${OPTIONS} $FILE v:$PATH

to copy a file from a Vagrant VM to the host:

scp ${OPTIONS} v:$PATH $FILE 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment