bash 用于从标签设置亚马逊 ec2 主机名的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12268986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
shell script to set amazon ec2 hostname from tags
提问by Reed
I am trying to set amazon EC2 hostname from the tag "Name"
我正在尝试从标签“名称”设置亚马逊 EC2 主机名
And found the answerto extract tags from instance data.
ec2-describe-tags \
--filter "resource-type=instance" \
--filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)" \
--filter "key=Name" | cut -f5
the result is:
结果是:
+------------+--------------+------+--------+
| resourceId | resourceType | key | value |
+------------+--------------+------+--------+
| i-1xxxxxxx | instance | Name | dev200 |
+------------+--------------+------+--------+
I can see that I am almost there, but how do I get the value(dev200) from the result above? Then I can use it in:
我可以看到我快到了,但是如何从上面的结果中获得 value(dev200) 呢?然后我可以将它用于:
echo $HOSTNAME > /etc/hostname
p.s. I have BASH on the instance, but I am completely lost in the bash document. can someone point me to the correct paragraph?
ps 我在实例上有 BASH,但我完全迷失在bash 文档中。有人可以指出我正确的段落吗?
回答by Reed
After some error and trial, got the script working:
经过一些错误和试验后,使脚本正常工作:
#!/bin/bash
hostname=`ec2-describe-tags --filter "resource-type=instance" \
--filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)" \
--filter "key=Name" | grep Name`
IFS="|" read -ra NAME <<< "$hostname"
hostname=${NAME[4]}
echo $hostname
Used IFS to get the string parsed into arrays, and luckily I know the 4th element is always the hostname.
使用 IFS 将字符串解析为数组,幸运的是我知道第 4 个元素始终是主机名。
EDIT (20-DEC-2012): In the short time since this was posted, several of the relevant ec2 command line tools have been modified, and flags changed or deprecated (e.g., the -i flag from above no longer seems to work on the current version of ec2metadata). Bearing that in mind, here is the command line script I used to get the current machine's "Name" tag (can't speak to the rest of the script):
编辑(2012 年 12 月 20 日):自发布以来的短时间内,一些相关的 ec2 命令行工具已被修改,并且标志已更改或已弃用(例如,上面的 -i 标志似乎不再适用于ec2metadata 的当前版本)。记住这一点,这是我用来获取当前机器的“名称”标签的命令行脚本(不能与脚本的其余部分对话):
ec2-describe-tags --filter "resource-type=instance" --filter "resource-id=$(ec2metadata --instance-id)" | awk '{print }'
On Debian/Ubuntu, you need to apt-get install cloud-utils ec2-api-toolsto get these working (the later is only on Ubuntu Multiverse).
在 Debian/Ubuntu 上,您需要apt-get install cloud-utils ec2-api-tools让这些工作(后者仅在 Ubuntu Multiverse 上)。
回答by Dennis
You could just use curl, since it is usually installed.
您可以只使用 curl,因为它通常已安装。
assigned_host_name=$(curl 'http://169.254.169.254/latest/meta-data/assigned_host_name') assigned_domain_name=$(curl 'http://169.254.169.254/latest/meta-data/assigned_domain_name')
assigned_host_name=$(curl ' http://169.254.169.254/latest/meta-data/assigned_host_name')assigned_domain_name=$(curl' http://169.254.169.254/latest/meta-data/assigned_domain_name')
Then just check that the values assigned don't contain a 404 HTML message like below.
然后只需检查分配的值是否不包含如下所示的 404 HTML 消息。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>404 - Not Found</title> </head> <body> <h1>404 - Not Found</h1> </body> </html>
Thenn do whatever magic you want to assign hostname. I'll edit this later today to add how I did that.
然后做任何你想分配主机名的魔法。我将在今天晚些时候编辑它以添加我是如何做到的。

