You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.7 KiB
89 lines
1.7 KiB
#!/bin/sh
|
|
|
|
do_start() {
|
|
#FILE=/usr/share/dict/hostname-list
|
|
if [ -f "/etc/hostname" ]; then
|
|
OLDHOST="$(cat /etc/hostname)"
|
|
else
|
|
OLDHOST="$(/bin/hostname)"
|
|
fi
|
|
|
|
if [ "${OLDHOST}" = "localhost" ]; then
|
|
ES=1
|
|
|
|
echo "Cannot randomize 'localhost', please change the hostname first to something sensible."
|
|
|
|
exit $ES
|
|
fi
|
|
|
|
# Make sure that our selected hostname doesn't appear anywhere in /etc,
|
|
# this way it should be safe to sed the whole directory
|
|
# we aren't doing this, but it could be useful in the future.
|
|
NOTFOUND=0
|
|
until [ "${NOTFOUND}" -gt "0" ]; do
|
|
#WORD=$(/usr/bin/shuf -n1 "$FILE")
|
|
if [ -e /usr/bin/openssl ]; then
|
|
WORD=$(/usr/bin/openssl rand -hex 8)
|
|
elif [ -e /usr/bin/hexdump ]; then
|
|
WORD=$(dd if=/dev/urandom bs=8 count=1 2>>/dev/null | /usr/bin/hexdump -v -e '/1 "%02x"')
|
|
elif [ -e /usr/bin/od ]; then
|
|
WORD=$(dd if=/dev/urandom bs=8 count=1 2>>/dev/null | /usr/bin/od -An -tx8 | tr -d ' ')
|
|
else
|
|
echo -e "\nERROR: Unable to set random hostname!\n"
|
|
exit 1
|
|
fi
|
|
ES=$?
|
|
if [ $ES -gt 0 ]; then
|
|
echo "Error getting random hostname!"
|
|
exit $ES
|
|
fi
|
|
|
|
/bin/fgrep -q -r $WORD /etc/ 2>&1 >> /dev/null
|
|
NOTFOUND=$?
|
|
done
|
|
|
|
echo "Setting hostname to '$WORD'"
|
|
|
|
/bin/hostname $WORD
|
|
|
|
ES=$?
|
|
|
|
echo "$WORD" > /etc/hostname
|
|
echo "$OLDHOST" > /etc/hostname.old
|
|
|
|
/bin/sed -i "s/${OLDHOST}/${WORD}/g" /etc/hosts
|
|
|
|
exit $ES
|
|
}
|
|
|
|
do_status() {
|
|
HOSTNAME=$(/bin/hostname)
|
|
|
|
if [ "$HOSTNAME" ] ; then
|
|
return 0
|
|
else
|
|
return 4
|
|
fi
|
|
}
|
|
|
|
case "$1" in
|
|
start|restart|"")
|
|
do_start
|
|
;;
|
|
reload|force-reload)
|
|
echo "Error: argument '$1' not supported" >&2
|
|
exit 3
|
|
;;
|
|
stop)
|
|
# No-op
|
|
;;
|
|
status)
|
|
do_status
|
|
exit $?
|
|
;;
|
|
*)
|
|
echo "Usage: hostname-spoof [start|status|stop]" >&2
|
|
exit 3
|
|
;;
|
|
esac
|