elvish_config/aliases/clean_known_hosts.elv

37 lines
914 B
Plaintext
Raw Normal View History

2020-03-13 14:03:36 +00:00
#alias:new clean_known_hosts e:cat
fn clean_known_hosts [@_args]{
2020-07-22 09:24:43 +00:00
use str
2020-03-13 14:03:36 +00:00
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]{
2020-07-22 09:24:43 +00:00
local:i1 = (condition:set (ip:is-ip $e1) 1 0)
local:i2 = (condition:set (ip:is-ip $e2) 1 0)
2020-03-13 14:03:36 +00:00
if (!= $i1 $i2) {
< $i1 $i2
} elif (== $i1 0) {
<s $e1 $e2
} else {
2020-07-22 09:24:43 +00:00
less $ip:cmp~ $e1 $e2
2020-03-13 14:03:36 +00:00
}
}
local:khosts = [&]
cat ~/.ssh/known_hosts | eawk [_ hosts @rest]{
2020-07-22 09:24:43 +00:00
local:key = (str:join ' ' $rest)
2020-03-13 14:03:36 +00:00
if (not (has-key $khosts $key)) {
khosts[$key] = [&]
}
2020-07-22 09:24:43 +00:00
str:split , $hosts | each [h]{
2020-03-13 14:03:36 +00:00
khosts[$key][$h] = $nil
}
}
local:lines = [(keys $khosts | each [key]{
2020-07-22 09:24:43 +00:00
local:hosts = (keys $khosts[$key] | order &less-than=$sort-host~ | str:join ,)
put $hosts $key | str:join ' '
2020-03-13 14:03:36 +00:00
})]
2020-07-22 09:24:43 +00:00
echo (str:join "\n" $lines) > ~/.ssh/known_hosts
2020-03-13 14:03:36 +00:00
}