-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.ssh_completion
55 lines (50 loc) · 2.23 KB
/
.ssh_completion
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
eval home=~/
export SSH_CONFIG_GENERATE_FILE="$home.ssh/config-generate"
export SSH_SHARED_DIR="$home.ssh/shared"
bash_directory=$( dirname "${BASH_SOURCE[0]}" )
export SSH_SHARED_CONFIG_FILE=$bash_directory/".ssh_config_shared_files"
if [ -f $SSH_SHARED_CONFIG_FILE ]; then
files=$(cat -s $SSH_SHARED_CONFIG_FILE | sed -e /^$/d | sed 's/^ *//;s/ *$//' | sed '/^#/ d' | tr '\n' ';' |sed s'/;$//')
export SSH_SHARED_FILES="$files"
fi
getsshsharedconfig() {
if [ ! -d "$SSH_SHARED_DIR" ]; then
mkdir -p "$SSH_SHARED_DIR"
fi
find $SSH_SHARED_DIR -mmin +5 -type f -delete 2> /dev/null
fileprefix=1
while IFS=';' read -ra ARR; do
for i in "${ARR[@]}"; do
configfile=$(echo $i | md5sum | cut -d\ -f1)
configfile=$(printf "%.3d-%s" $fileprefix $configfile)
if [ ! -f "$configfile.config" ]; then
configcontent=$(wget -q -O - "$@" $i)
ret=$?
if [ $ret == 0 ]; then
echo "$configcontent" > "$SSH_SHARED_DIR/$configfile.config"
fi
fi
fileprefix=`expr $fileprefix + 1`
done
done <<< "$SSH_SHARED_FILES"
}
compilesshconfig() {
echo -n > $SSH_CONFIG_GENERATE_FILE
echo -e "###############################\n# file generate do not modify #\n###############################\n\n" > $SSH_CONFIG_GENERATE_FILE
echo -e "\n### config-before ###\n\n" >> $SSH_CONFIG_GENERATE_FILE
cat ~/.ssh/config-before 2> /dev/null >> $SSH_CONFIG_GENERATE_FILE
echo -e "\n### config ###\n\n" >> $SSH_CONFIG_GENERATE_FILE
cat ~/.ssh/config 2> /dev/null >> $SSH_CONFIG_GENERATE_FILE
echo -e "\n### configs files ###\n\n" >> $SSH_CONFIG_GENERATE_FILE
cat $SSH_SHARED_DIR/*.config 2> /dev/null >> $SSH_CONFIG_GENERATE_FILE
echo -e "\n### config-after ###\n\n" >> $SSH_CONFIG_GENERATE_FILE
cat ~/.ssh/config-after 2> /dev/null >> $SSH_CONFIG_GENERATE_FILE
}
alias ssh='getsshsharedconfig;compilesshconfig && ssh -F $SSH_CONFIG_GENERATE_FILE'
_ssh()
{
$(getsshsharedconfig;compilesshconfig)
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(cat $SSH_CONFIG_GENERATE_FILE | grep "Host " | grep -v "*" | awk '{print $2}' | tr '\n' ' ')" -- $cur) )
}
complete -F _ssh ssh