Posted to tcl by rmax at Fri Dec 22 09:17:00 GMT 2017view raw

  1. #!/usr/bin/tclsh
  2. # -*- Tcl -*-
  3.  
  4. package require Expect
  5.  
  6. set prompt ":~> "
  7. set keyfile "~/.ssh/id_rsa.pub"
  8. set needcopy 0
  9.  
  10. spawn ssh $argv
  11.  
  12. expect {
  13. {[Pp]ass*: } {
  14. set needcopy 1
  15. interact "\r" {
  16. send "\r"
  17. exp_continue
  18. }
  19. }
  20. $prompt
  21. }
  22.  
  23. if {$needcopy} {
  24. set fd [open $keyfile]
  25. gets $fd pubkey
  26. close $fd
  27. send " mkdir -p ~/.ssh\r"
  28. expect $prompt
  29. send " cat >> ~/.ssh/authorized_keys <<EOF\r$pubkey\rEOF\r"
  30. expect $prompt
  31. }
  32. interact
  33.