Vagrant VM 中 SSH 和 运行 命令的别名

Alias for SSH and run command in a Vagrant VM

我在一个文件夹中有 2 台 vagrant 机器,我经常不得不在这两台机器中使用 ssh 和 运行 相同的命令。基本上我是这样的:

$ 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

我想创建一个别名或一个小脚本,我可以 运行 它会做这些事情,所以我不需要一次又一次地输入所有这些...
有什么想法吗?

为此,您可以添加别名:

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

将此添加到您的个人资料(即 ~/.bash_profile 或类似文件),然后按 运行:

重新加载
. ~/.bash_profile

如果你想让它真的很可爱,你可以这样做:

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

它适用于 1 到 n 个盒子的任何 Vagrant 设置,运行 该命令针对当前启动的所有盒子。