-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-hostname.sh
More file actions
executable file
·128 lines (112 loc) · 4.81 KB
/
setup-hostname.sh
File metadata and controls
executable file
·128 lines (112 loc) · 4.81 KB
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
#!/bin/bash
################################################################################
# 24/1/17
# setup linux OS name using new variable name NICKNAME.
# this allow us to save the underline hostname used.
# example: ip-172-12-12-3.eu-west-1.compute.internal.
# This is intended to run on an Amazon EC2 instance and require sudo
# permission and hostname argument supplied.
# Tested on Ubnutu and rhel.
# (c) 2017 Sharon Mafgaoker, all rights reserved;
# You are free to use, modify and redistribute this software in any form
# under the conditions described in the LICENSE file included.
#################################################################################
# Function for checking if script running as root, if not terminating.
func_check_for_root() {
if [ ! $( id -u ) -eq "0" ]; then
echo "ERROR: $0 Must be run as root, Script terminating."; exit 7
fi
}
func_check_for_root
# Check existence of input argument.
if [ "$#" -eq "0" ] ; then
echo "ERROR: No hostname argument supplied, Script terminating."; exit 7
fi
# set the name
HOSTNAME="$1"
if [ -d /etc/profile.d ]; then
echo "export NICKNAME=$HOSTNAME" > /etc/profile.d/prompt.sh
else
echo "ERROR: /etc/profile.d directory does not exist or you don't have permission to edit it"
exit 7
fi
func_run_for_ubuntu(){
# Replace PS1 \h with $NICKNAME in every /home/username/.bashrc file
for i in /home/*
do
if [ -e "$i/.bashrc" ]; then
if ! grep -q '\\u@$NICKNAME' "$i/.bashrc"; then
echo "\$NICKNAME does not exists in $i/.bashrc I'm going to create it."
# backup the original file /home/username/.bashrc , and replace \h with $NICKNAME
sed -i.bak '/\u@\\h/s/\\h/$NICKNAME/g' "$i/.bashrc" > /dev/null
fi
else
echo "ERROR: $i/.bashrc File does not exists or you don't have permission to edit the file"
fi
done
# Replace PS1 \h in /etc/bash.bashrc with $NICKNAME, substitutions using sed
if [ -e /etc/bash.bashrc ]; then
if ! grep -q '\\u@$NICKNAME' /etc/bash.bashrc; then
echo "\$NICKNAME not exists in /etc/bash.bashrc, I'm going to create it."
# backup the original file /etc/bash.bashrc , and replace \h with $NICKNAME
sed -i.bak '/PS1=/s/\\h/$NICKNAME/' /etc/bash.bashrc > /dev/null
fi
if ! grep -q '/etc/profile.d/prompt.sh' /etc/bash.bashrc; then
echo "/etc/profile.d/prompt.sh not exists in /etc/bash.bashrc, I'm going to add it."
# Execute the shell scripts /etc/profile.d/prompts.sh if the users shell is Login Shell.
echo "# Execute the shell scripts /etc/profile.d/prompts.sh for Non-interactive Shells." >> /etc/bash.bashrc
echo ". /etc/profile.d/prompt.sh" >> /etc/bash.bashrc
fi
else
echo "ERROR: /etc/bash.bashrc File does not exists or you don't have permission to edit the file"
exit 7
fi
echo "You are ready to go, please logout to see the changes."
return 0
# Source env variable , to get immediate effect. not set for now.
}
func_run_for_rhel(){
# sets the terminal title to username@hostname: directory:
echo '''echo -ne "\033]0;${USER}@${NICKNAME}: ${PWD}\007"''' > /etc/sysconfig/bash-prompt-xterm
chmod +x /etc/sysconfig/bash-prompt-xterm
# Patterns to find in the file
# '&&\sPS1="\[[email protected]\s'
if grep -q '&&\sPS1="\[.u@.$NICKNAME\s' /etc/bashrc; then
exit 0
fi
# backup the original file /etc/bashrc
# Replace PS1 h in /etc/bashrc with $NICKNAME ,substitutions using sed
if [ -e /etc/bashrc ]; then
sed -i.bak '/&&\sPS1="\[[email protected]\s/s/h/$NICKNAME/' /etc/bashrc > /dev/null
else
echo "ERROR: /etc/bashrc File does not exist or you don't have permission to edit the file"
exit 1
fi
echo "You are ready to go, please logout to see the changes."
return 0
}
# Getting the distribution name, checking for Ubuntu or Redhat.
UNAME=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [ "$UNAME" == "linux" ]; then
if [ -f /etc/redhat-release ]; then
DISTRO="rhel"
# If available, use LSB to identify distribution
elif [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'// | tr "[:upper:]" "[:lower:]")
elif [ -f /etc/os-release ]; then
DISTRO=$(awk -F= '/^ID=/{print $2}' /etc/os-release | sed s/'"'//g)
else
echo "ERROR: No linux distro found"; exit 7
fi
fi
echo "$DISTRO found"
# check if Distro Ubuntu or Redhat.
if [ "$DISTRO" == "ubuntu" ]; then
func_run_for_ubuntu
elif [ "$DISTRO" == "rhel" -o "$DISTRO" == "redhatenterpriseserver" ]; then
func_run_for_rhel
else
echo "ERROR: No Ubuntu found"; exit 7
fi
exit 0