ssh.plugin.zsh
1636 bytes
1############################################################
2# Take all host sections in .ssh/config and offer them for
3# completion as hosts (e.g. for ssh, rsync, scp and the like)
4# Filter out wildcard host sections.
5_ssh_configfile="$HOME/.ssh/config"
6if [[ -f "$_ssh_configfile" ]]; then
7 _ssh_hosts=($(
8 grep -E '^Host.*' "$_ssh_configfile" |\
9 awk '{for (i=2; i<=NF; i++) print $i}' |\
10 sort |\
11 uniq |\
12 grep -v '^*' |\
13 sed -e 's/\.*\*$//'
14 ))
15 zstyle ':completion:*:hosts' hosts $_ssh_hosts
16 unset _ssh_hosts
17fi
18unset _ssh_configfile
19
20############################################################
21# Remove host key from known hosts based on a host section
22# name from .ssh/config
23function ssh_rmhkey {
24 local ssh_configfile="$HOME/.ssh/config"
25 local ssh_host="$1"
26 if [[ -z "$ssh_host" ]]; then return; fi
27 ssh-keygen -R $(grep -A10 "$ssh_host" "$ssh_configfile" | grep -i HostName | head -n 1 | awk '{print $2}')
28}
29compctl -k hosts ssh_rmhkey
30
31############################################################
32# Load SSH key into agent
33function ssh_load_key() {
34 local key="$1"
35 if [[ -z "$key" ]]; then return; fi
36 local keyfile="$HOME/.ssh/$key"
37 local keysig=$(ssh-keygen -l -f "$keyfile")
38 if ( ! ssh-add -l | grep -q "$keysig" ); then
39 ssh-add "$keyfile"
40 fi
41}
42
43############################################################
44# Remove SSH key from agent
45function ssh_unload_key {
46 local key="$1"
47 if [[ -z "$key" ]]; then return; fi
48 local keyfile="$HOME/.ssh/$key"
49 local keysig=$(ssh-keygen -l -f "$keyfile")
50 if ( ssh-add -l | grep -q "$keysig" ); then
51 ssh-add -d "$keyfile"
52 fi
53}