繁体   English   中英

SSH 无法解析 bash 脚本中的主机名

[英]SSH could Not Resolve Hostname in bash script

我已经使用 terraform 在 AWS 上部署了一个项目,并且我正在尝试使用脚本使 ssh 更容易进入我正在使用的 EC2 实例中。

I have a terraform output called host which contains the public dns for the EC2 instance and an output called host_ssh_key which contains the private key.

我使用的 bash 脚本如下:

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

tmp_ssh_file="$(mktemp)"

terraform output host_ssh_key > $tmp_ssh_file

chmod 400 $tmp_ssh_file

ssh -i $tmp_ssh_file ubuntu@$(terraform output host)

当我尝试使用./ssh-host.sh运行此脚本时,出现以下错误:

ssh: Could not resolve hostname "ec2-xx-xx-xxx-xxx.compute-x.amazonaws.com": nodename nor servname provided, or not known (没有x s)

但是,当我将密钥转储到文件中并将其与错误消息中的主机名一起使用时,我可以很好地连接到它,例如

terraform output host_ssh_key > private_key.pem
chmod 400 private_key.pem
ssh -i private_key.pem ubuntu@ec2-xx-xx-xxx-xxx.compute-x.amazonaws.com 

有谁知道这里可能会发生什么? 或者如何解决?

对于稍后回顾这个问题的人来说,问题是 terraform 将 output 用双引号括起来"以解决问题,我将最后一行修改为:

ssh -i $tmp_ssh_file ubuntu@$(terraform output --raw host)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM