2021-12-11 21:09:02 +00:00
|
|
|
edit:add-var cleankh~ {|@argv|
|
2021-08-24 18:45:25 +00:00
|
|
|
use str
|
|
|
|
use moi/util/condition
|
|
|
|
use moi/util/list
|
|
|
|
use moi/util/ip
|
2021-12-11 21:09:02 +00:00
|
|
|
fn less {|cmp e1 e2| < ($cmp $e1 $e2) 0 }
|
|
|
|
fn sort-host {|e1 e2|
|
2021-08-27 19:45:39 +00:00
|
|
|
var i1 = (condition:cset (ip:is-ip $e1) 1 0)
|
|
|
|
var i2 = (condition:cset (ip:is-ip $e2) 1 0)
|
2021-08-24 18:45:25 +00:00
|
|
|
if (!= $i1 $i2) {
|
|
|
|
< $i1 $i2
|
|
|
|
} elif (== $i1 0) {
|
|
|
|
<s $e1 $e2
|
|
|
|
} else {
|
|
|
|
less $ip:cmp~ $e1 $e2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 19:45:39 +00:00
|
|
|
var khosts = [&]
|
2021-12-11 21:09:02 +00:00
|
|
|
cat ~/.ssh/known_hosts | eawk {|_ hosts @rest|
|
2021-08-27 19:45:39 +00:00
|
|
|
var key = (str:join ' ' $rest)
|
2021-08-24 18:45:25 +00:00
|
|
|
if (not (has-key $khosts $key)) {
|
2021-10-13 18:43:40 +00:00
|
|
|
set khosts[$key] = [&]
|
2021-08-24 18:45:25 +00:00
|
|
|
}
|
2021-12-11 21:09:02 +00:00
|
|
|
str:split , $hosts | each {|h|
|
2021-10-13 18:43:40 +00:00
|
|
|
set khosts[$key][$h] = $nil
|
2021-08-24 18:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
var lines = [(keys $khosts | each {|key|
|
2021-08-27 19:45:39 +00:00
|
|
|
var hosts = (keys $khosts[$key] | order &less-than=$sort-host~ | str:join ,)
|
2021-08-24 18:45:25 +00:00
|
|
|
put $hosts $key | str:join ' '
|
|
|
|
})]
|
|
|
|
echo (str:join "\n" $lines) > ~/.ssh/known_hosts
|
|
|
|
}
|
|
|
|
|