【问题标题】:SSH could Not Resolve Hostname in bash scriptSSH 无法解析 bash 脚本中的主机名
【发布时间】:2022-01-25 04:45:39
【问题描述】:

我已经使用 terraform 在 AWS 上部署了一个项目,我正在尝试使用脚本来更轻松地通过 ssh 连接到我正在使用的 EC2 实例。

我有一个名为 host 的 terraform 输出,其中包含 EC2 实例的公共 dns,还有一个名为 host_ssh_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(没有xs)

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

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 

有人知道这里会发生什么吗?或者如何解决?

【问题讨论】:

  • 如果你将输出重定向到一个文件,如果你使用ubuntu@$(cat filename)会发生什么?
  • 不,我的意思是terraform output host > test 然后ssh -i $tmp_ssh_file ubuntu@$(cat test)
  • 您还可以使用十六进制转储检查test 文件,以查看是否有任何非打印字符。
  • 啊,是的,我写完愚蠢的回复后才明白你的意思????查看文件是因为它用引号括起来 - 这在错误消息中也没有显得格格不入。感谢您的帮助!
  • 我傻了,我只是对未知主机做了我自己的ssh,并没有注意到错误消息没有引号。

标签: bash amazon-web-services amazon-ec2 ssh terraform


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多