0

I have 2 vagrant machines in a folder, and I'm constantly having to ssh in both and run the same command. Basically I do:

$ vagrant ssh machine1
[vagrant@machine1 ~]$ sudo rm -rf /tmp/cache/*
[vagrant@machine1 ~]$ exit

$ vagrant ssh machine2
[vagrant@machine2 ~]$ sudo rm -rf /tmp/cache/*
[vagrant@machine2 ~]$ exit

I'd like to create an alias or a little script I can run and it will do those things, so I don't need to type all that again and again...
Any ideas?

2 Answers 2

2

To do this, you can add an alias:

alias vssh="vagrant ssh machine1 -c 'sudo rm -rf /tmp/cache/*'; vagrant ssh machine2 -c 'sudo rm -rf /tmp/cache/*'"

Add this to your profile (i.e. ~/.bash_profile or similar file), then reload it by running:

. ~/.bash_profile
0
1

If you wanted to be really cute about it, you could do this:

alias vagrantsshall="function _() { for box in \`vagrant status --machine-readable | sed -nE 's/[0-9]+,([^,]+),state,running/\1/p'\`; do echo $box: && vagrant ssh $box -c \$1; done;}; _"

It would work for any Vagrant setup of 1 to n boxes, running the command against all the boxes that are currently up.

Not the answer you're looking for? Browse other questions tagged or ask your own question.