elvish_config/aliases/cleankh.elv

36 lines
919 B
Plaintext
Raw Normal View History

2021-12-11 21:09:02 +00:00
edit:add-var cleankh~ {|@argv|
2021-08-24 18:45:25 +00:00
use str
use framagit.org/benjamin.vaudour/elv-lib/mods/common
use framagit.org/benjamin.vaudour/elv-lib/mods/ip
use framagit.org/benjamin.vaudour/elv-lib/mods/list
2021-12-11 21:09:02 +00:00
fn sort-host {|e1 e2|
var i1 = (common:cond (ip:is-ip $e1) 1 0)
var i2 = (common:cond (ip:is-ip $e2) 1 0)
2021-08-24 18:45:25 +00:00
if (!= $i1 $i2) {
- $i1 $i2
2021-08-24 18:45:25 +00:00
} elif (== $i1 0) {
compare $e1 $e2
2021-08-24 18:45:25 +00:00
} else {
ip:comp $e1 $e2
2021-08-24 18:45:25 +00:00
}
}
var khosts = [&]
2021-12-11 21:09:02 +00:00
cat ~/.ssh/known_hosts | eawk {|_ hosts @rest|
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|
var hosts = (keys $khosts[$key] | list:sort $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
}