This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathnet-15-hostname
executable file
·147 lines (124 loc) · 4.09 KB
/
net-15-hostname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
_kernel="$(uname -s)"
if [ "${_kernel}" = 'FreeBSD' ]; then
SED_I="sed -i ''"
else
SED_I="sed -i''"
fi
function set_hostname() {
local hostname=$1
if [ -d /run/systemd/system/ ]; then
hostnamectl set-hostname --static "${hostname}"
else
if [ -f /etc/sysconfig/network ]; then
${SED_I} '/^HOSTNAME=.*$/d' /etc/sysconfig/network
echo "HOSTNAME=${hostname}" >>/etc/sysconfig/network
elif [ "${_kernel}" = 'FreeBSD' ]; then
sysrc hostname="${hostname}"
else
echo "${hostname}" >/etc/hostname
fi
hostname "${hostname}"
fi
}
function set_domainname() {
domain=$1
${SED_I} -e '/^domain .*/d' /etc/resolv.conf
echo "domain ${domain}" >>/etc/resolv.conf
}
function get_first_ip() {
local ip
ip=${ip:-$(ip route get 1 2>/dev/null | grep 'src [0-9\.]\+' | head -1 | sed -e 's/^.*src \([0-9\.]*\).*$/\1/')}
ip=${ip:-$(ip -4 address show scope global up 2>/dev/null | awk '/inet / { gsub(/\/[^\/]+$/, "", $2); print $2; exit}')}
ip=${ip:-$(ifconfig 2>/dev/null | awk '/inet / { gsub(/\/[^\/]+$/, "", $2); print $2; exit}')}
ip=${ip:-$(hostname -I 2>/dev/null | cut -d' ' -f1)}
ip=${ip:-$(hostname -i 2>/dev/null)}
echo "${ip}"
}
function get_dns_name() {
text=$(LC_ALL=C host "$1" 2>/dev/null)
[ $? = 0 ] || exit 0
[[ $text == *"has no PTR record" ]] && exit 0
name=$(echo "$text" | awk '/(has address|name pointer)/ {print $(NF)}' | sed 's/\.$//')
echo $name
}
function update_hosts() {
ip=$1
name=$2
hostname=$3
if [ "x${hostname}" = "x${name}" ]; then
hosts="${name}"
else
hosts="${name} ${hostname}"
fi
note='# one-contextd'
entry="${ip} ${hosts} ${note}"
# update our old entry
if grep -qi "${note}" /etc/hosts; then
${SED_I} -e "s/^.*${note}\$/${entry}/" /etc/hosts
# update entry with same IP (but not localhost)
elif grep -E "^${ip}[[:space:]]" /etc/hosts | grep -qv localhost; then
${SED_I} -e "/localhost/! s/^${ip}[[:space:]].*\$/${entry}/" /etc/hosts
# update entry with same name
elif grep -qE "[[:space:]]${name}([[:space:]]|#|\$)" /etc/hosts; then
${SED_I} -re "s/^.*[[:space:]]${name}([[:space:]#].*|$)/${entry}/" /etc/hosts
# create new entry
elif [ -f /etc/hosts ]; then
${SED_I} -e "1s/^/${entry}"$'\\\n/' /etc/hosts
else
echo "${entry}" >>/etc/hosts
fi
}
#####
first_ip=$(get_first_ip)
if [ -n "$SET_HOSTNAME" ]; then
name=$(echo "$SET_HOSTNAME" | \
sed -e 's/[^-a-zA-Z0-9\.]/-/g' -e 's/^-*//g' -e 's/-*$//g')
elif [ -n "$DNS_HOSTNAME" ]; then
name=$(get_dns_name "${first_ip}")
elif [ "${EC2_HOSTNAME}" = 'YES' ]; then
# try to quickly get hostname from the EC2 metadata server or
# create hostname based on the first IPv4 (format: "ip-1-2-3-4")
name=$(curl -sf -m 5 'http://169.254.169.254/latest/meta-data/local-hostname' 2>/dev/null)
if [ -z "${name}" ]; then
name="$(echo "${first_ip}" | grep -x '[0-9\.]\+' | tr . -)"
if [ -n "${name}" ]; then
name="ip-${name}"
fi
fi
fi
if [ -n "${name}" ]; then
# split host and domain names
hostname=${name%%.*}
domain=${name#*.}
if [ "x${domain}" = "x${hostname}" ]; then
domain=''
fi
# FreeBSD
if [ "${_kernel}" = 'FreeBSD' ]; then
set_hostname "${name}"
else
set_hostname "${hostname}"
fi
if [ -n "${domain}" ]; then
set_domainname "${domain}"
fi
if [ -n "${DNS_HOSTNAME}" ]; then
host_ip=$first_ip
else
# If selected hostname resolves on first IP,
# use first IP for local hostname in /etc/hosts.
# Otherwise use loopback IP.
name_ip=$(get_dns_name "${name}")
if [ "x${first_ip}" = "x${name_ip}" ]; then
host_ip=$first_ip
elif [ -f /etc/debian_version ]; then
host_ip='127.0.1.1'
else
host_ip='127.0.0.1'
fi
fi
if [ -n "${host_ip}" ]; then
update_hosts "${host_ip}" "${name}" "${hostname}"
fi
fi