如何在不实际运行“vagrant ssh"的情况下 ssh 到 vagrant? [英] How to ssh to vagrant without actually running "vagrant ssh"?

查看:37
本文介绍了如何在不实际运行“vagrant ssh"的情况下 ssh 到 vagrant?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ssh 命令在 shell 脚本中重现 Vagrant 登录到我的 VM 的方式,因此我为我的 Vagrant 实例创建了一个别名.

I would like to reproduce the way Vagrant logs in to my VM within a shell script using an ssh command, so I create an alias to my Vagrant instance.

使用常规 ssh 命令访问它的命令语法是什么?

What is the command syntax to use the regular ssh command to access it?

推荐答案

我不得不重新实现vagrant ssh",因为它的 -c 选项没有正确传递参数.这基本上就是它的作用(可能还有更多,但这样可以很好地工作)

I've had to re-implement "vagrant ssh" because it's -c option didn't pass on arguments properly. This is basically what it does (there might be more, but it works fine this way)

#!/bin/sh
PORT=$(vagrant ssh-config | grep Port | grep -o '[0-9]+')
ssh -q 
    -o UserKnownHostsFile=/dev/null 
    -o StrictHostKeyChecking=no 
    -i ~/.vagrant.d/insecure_private_key 
    vagrant@localhost 
    -p $PORT 
    "$@"

作为单行(感谢kgadek):

As a one-liner (with thanks to kgadek):

ssh $(vagrant ssh-config | awk 'NR>1 {print " -o "$1"="$2}') localhost

考虑到当您有多个流浪主机时,这将选择所需的主机,并从配置中剔除空行(使用 sed):

To account for when you have more than one vagrant host, this will select the desired host, as well as cull blank lines from the config (using sed):

HOST=name-of-my-host
ssh $(vagrant ssh-config $HOST | sed '/^[[:space:]]*$/d' |  awk 'NR>1 {print " -o "$1"="$2}') localhost

这篇关于如何在不实际运行“vagrant ssh"的情况下 ssh 到 vagrant?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆