-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-to-ssh-config.sh
executable file
·64 lines (55 loc) · 1.34 KB
/
add-to-ssh-config.sh
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
#!/bin/bash
##
## addhost.sh
##
## Made by heow
## Login <[email protected]>
##
## Started on Thu Jun 5 14:09:04 2008 heow
## Last update Thu Jun 5 14:09:04 2008 heow
##
if [ -z "$2" ] ; then
echo "usage: $0 ipaddress hostname"
exit 1
fi
export HOSTFILE="${HOME}/.ssh/config"
export IPADDR=$1
export HOSTNAME=$2
export TMPFILE=`mktemp addhost.XXXXXXX`
# check each line of output to see if...
export EDITED=0
export HOSTFOUND=0
while read line ; do
# is line commented out?
if [ "#" == "`echo \"\${line%${line#?}}\"`" ] ; then
echo "$line" >> $TMPFILE
continue;
fi
# found host?
if echo $line | grep -i "Host.*${HOSTNAME}" > /dev/null ; then
# echo $* >> $TMPFILE
# export EDITED=1
export HOSTFOUND=1
echo "${line}" >> $TMPFILE
continue
fi
# in the correct host, overwrite the next hostname (with IPADDR)
if [ 1 == $HOSTFOUND ]; then
if echo $line | grep -i Hostname > /dev/null ; then
# echo "#${line}" >> $TMPFILE
echo "Hostname ${IPADDR}" >> $TMPFILE
export EDITED=1
export HOSTFOUND=0
continue
fi
fi
# everything else
echo $line >> $TMPFILE
done < $HOSTFILE
if [ 0 == $EDITED ] ; then
echo "" >> $TMPFILE
echo "Host ${HOSTNAME}" >> $TMPFILE
echo "Hostname ${IPADDR}" >> $TMPFILE
fi
cp $TMPFILE $HOSTFILE
rm $TMPFILE