elvish_config/aliases/clean_known_hosts.elv

38 lines
1010 B
Plaintext

#alias:new clean_known_hosts e:cat
fn clean_known_hosts [@_args]{
use moi/util/condition
use moi/util/list
use moi/util/ip
fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 }
fn sort-host [e1 e2]{
local:i1 = (condition:set (ip:is-ipv4 $e1) 2 (condition:set (ip:is-ipv6 $e1) 1 0))
local:i2 = (condition:set (ip:is-ipv4 $e2) 2 (condition:set (ip:is-ipv6 $e2) 1 0))
if (!= $i1 $i2) {
< $i1 $i2
} elif (== $i1 0) {
<s $e1 $e2
} elif (== $i1 1) {
less $ip:cmp6~ $e1 $e2
} else {
less $ip:cmp4~ $e1 $e2
}
}
local:khosts = [&]
cat ~/.ssh/known_hosts | eawk [_ hosts @rest]{
local:key = (joins ' ' $rest)
if (not (has-key $khosts $key)) {
khosts[$key] = [&]
}
splits , $hosts | each [h]{
khosts[$key][$h] = $nil
}
}
local:lines = [(keys $khosts | each [key]{
local:hosts = (keys $khosts[$key] | list:sort $sort-host~ | joins ,)
put $hosts $key | joins ' '
})]
echo (joins "\n" $lines) > ~/.ssh/known_hosts
}