Advertisement
shinemic

copy-ssh-id batch scripts (expect/tcl)

May 4th, 2022
581
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/expect -f
  2.  
  3. set timeout 5
  4. set ip_cnt 0
  5. log_user 0
  6.  
  7. puts ">>> Start adding keys <<<"
  8.  
  9. set fh [open [lindex $argv 0]]
  10.  
  11. while {[gets $fh line] >= 0} {
  12.     set info   [ split  $line   ]
  13.     set ip     [ lindex $info 0 ]
  14.     set passwd [ lindex $info 1 ]
  15.  
  16.     puts -nonewline [format "#%-3d adding keys for %-15s ...... " [incr ip_ct] $ip]
  17.     flush stdout
  18.  
  19.     spawn -noecho ssh-copy-id root@$ip
  20.  
  21.     expect {
  22.         timeout { puts "time out" }
  23.  
  24.         "yes/no" {
  25.             send "yes\r"
  26.             expect "password:" { send "$passwd\r" }
  27.         }
  28.  
  29.         "password:" {
  30.             send "$passwd\r"
  31.             expect {
  32.                 "key(s) added: "    { puts "success" }
  33.                 "Permission denied" { puts "wrong password"; send "\003" }
  34.             }
  35.         }
  36.  
  37.         "skipped" { puts "already installed!" }
  38.     }
  39. }
  40.  
  41. close $fh
  42.  
  43. puts ">>> Done <<<"
Advertisement
RAW Paste Data Copied
Advertisement