Posted to tcl by aspect at Fri Jul 30 05:07:50 GMT 2010view raw

  1. #!/usr/bin/tclsh
  2. # save as test.tcl
  3. # run with same arguments as telnet
  4. # see expect manual for expect_after, timeout, log_user, interact
  5.  
  6. package require Expect
  7.  
  8. set timeout 2
  9.  
  10. expect_after timeout {
  11. puts "Expect timed out!"
  12. exit -1
  13. }
  14.  
  15. log_user 0
  16.  
  17. puts -nonewline "Connecting to $argv: "
  18.  
  19. # tcl8.6: spawn telnet {*}$argv
  20. eval spawn telnet $argv
  21.  
  22. expect {
  23. Connected {
  24. puts "Connected"
  25. log_user 1
  26. interact
  27. }
  28. "Connection refused" {
  29. puts "Error connecting!"
  30. exit -2
  31. }
  32. }
  33.  
  34. puts "Disconnected"