Create a gist now

Instantly share code, notes, and snippets.

@geedew /vagrant-scp
Last active Jul 22, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
Owner

geedew commented Apr 25, 2014

@guibog

This comment has been minimized.

Show comment
Hide comment
@guibog

guibog 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}'`

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

This comment has been minimized.

Show comment
Hide comment
@mykter

mykter 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 

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