2024-06-16 10:18:51 +00:00
|
|
|
edit:add-var clean-known-hosts~ {|@argv|
|
2024-02-11 18:37:36 +00:00
|
|
|
use re
|
2021-08-24 18:45:25 +00:00
|
|
|
use str
|
2021-12-27 17:54:26 +00:00
|
|
|
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
|
|
|
|
|
2022-02-19 09:56:19 +00:00
|
|
|
var hosts = [&]
|
|
|
|
var ips = [&]
|
|
|
|
var ids = [&]
|
|
|
|
var file = $E:HOME/.ssh/known_hosts
|
|
|
|
|
|
|
|
fn readfile {
|
|
|
|
cat $file | each {|l|
|
|
|
|
if (not (str:has-prefix $l '#')) {
|
|
|
|
put $l
|
|
|
|
}
|
2024-02-11 18:37:36 +00:00
|
|
|
} | re:awk {|_ host @rest|
|
2022-02-19 09:56:19 +00:00
|
|
|
var id = (str:join ' ' $rest)
|
|
|
|
var ihosts = []
|
|
|
|
str:split ',' $host | each {|h|
|
|
|
|
set @ihosts = $@ihosts $h
|
|
|
|
if (ip:is-ip $h) {
|
|
|
|
if (not (has-key $ips $h)) {
|
|
|
|
set ips[$h] = [ $id ]
|
|
|
|
} else {
|
|
|
|
set ips[$h] = [ (all $ips[$h]) $id ]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (not (has-key $hosts $h)) {
|
|
|
|
set hosts[$h] = [ $id ]
|
|
|
|
} else {
|
|
|
|
set hosts[$h] = [ (all $hosts[$h]) $id ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn searchips {|id|
|
|
|
|
if (has-key $ids $id) {
|
|
|
|
each {|h|
|
|
|
|
if (ip:is-ip $h) {
|
|
|
|
put $h
|
|
|
|
}
|
|
|
|
} $ids[$id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hashost {|id|
|
|
|
|
if (has-key $ids $id) {
|
|
|
|
list:contains {|h| not (ip:is-ip $h) } $ids[$id]
|
2021-08-24 18:45:25 +00:00
|
|
|
} else {
|
2022-02-19 09:56:19 +00:00
|
|
|
put $false
|
2021-08-24 18:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 09:56:19 +00:00
|
|
|
fn formatlines {
|
|
|
|
keys $hosts | order | each {|h|
|
|
|
|
order $hosts[$h] | each {|id|
|
|
|
|
var hid = (str:join ',' [ $h (searchips $id) ])
|
|
|
|
put $hid $id | str:join ' '
|
|
|
|
}
|
2021-08-24 18:45:25 +00:00
|
|
|
}
|
2022-02-19 09:56:19 +00:00
|
|
|
keys $ips | list:sort $ip:comp~ | each {|ip|
|
|
|
|
order $ips[$ip] | each {|id|
|
|
|
|
if (not (hashost $id)) {
|
|
|
|
put $ip $id | str:join ' '
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 18:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 09:56:19 +00:00
|
|
|
|
|
|
|
readfile
|
|
|
|
var @lines = (formatlines)
|
|
|
|
echo (str:join "\n" $lines) > $file
|
2021-08-24 18:45:25 +00:00
|
|
|
}
|