Bulk add remote host fingerprint in ~/.ssh/known_hosts using expect script

Script file below,

#!/usr/bin/expect -f
# adds server to known hosts list
# file containing list of hostnames is passed as 1st arg

set host_file [lindex $argv 0]

set f [open $host_file]
set hosts [split [read $f] "\n"]
close $f

foreach host $hosts {
    if { $host != "" } {
        spawn ssh $host
        expect "yes/no)?"
        send "yes\r"
        expect "list of known hosts."
        # send ctrl-c
        send \x03
    }
}

References
https://en.wikipedia.org/wiki/Expect

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *