35 lines
811 B
Bash
Executable File
35 lines
811 B
Bash
Executable File
#!/bin/sh
|
|
|
|
set -e
|
|
|
|
case $# in
|
|
0)
|
|
uci -q get 'system.@system[0].pretty_hostname' || uci get 'system.@system[0].hostname'
|
|
;;
|
|
1)
|
|
local pretty_hostname="$1"
|
|
# Remove invalid characters and leading/trailing spaces
|
|
local hostname="$(echo -n "$pretty_hostname" | sed -r -e 's/[^a-zA-Z0-9-]//g' -e 's/-+/-/g' -e 's/^-//')"
|
|
hostname="${hostname:0:63}"
|
|
hostname="$(echo -n "$hostname" | sed -e 's/-$//')"
|
|
|
|
if [ -z "$hostname" ]; then
|
|
hostname='localhost'
|
|
fi
|
|
|
|
if [ "$hostname" = "$pretty_hostname" ]; then
|
|
uci -q delete "system.@system[0].pretty_hostname"
|
|
else
|
|
uci set "system.@system[0].pretty_hostname=$pretty_hostname"
|
|
fi
|
|
|
|
uci set "system.@system[0].hostname=$hostname"
|
|
uci commit system
|
|
|
|
echo "$hostname" > /proc/sys/kernel/hostname
|
|
;;
|
|
*)
|
|
echo 'Usage: pretty-hostname [ hostname ]'
|
|
exit 1
|
|
esac
|