StartScreen 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/sh
  2. # -*- tcl -*- \
  3. exec expect -f "$0" "$@"
  4. set DefaultUser wr
  5. set DefaultPort 22
  6. set timeout 120
  7. proc usage {arg} {
  8. global argv0 DefaultUser DefaultPort
  9. if {$arg != ""} {
  10. puts "error: ${arg}"
  11. }
  12. puts "usage: ${argv0} {--verbose} {--cd=dir} {--login=user} host{:port}"
  13. puts " default user = ${DefaultUser}"
  14. puts " default port = ${DefaultPort}"
  15. exit 1
  16. }
  17. if {[llength $argv] <= 0} {
  18. usage "missing arguments"
  19. }
  20. # assumes the command will execute non-interactively
  21. proc probe {user host port dir} {
  22. spawn ssh -l $user -p $port $host
  23. set rc 0
  24. set ErrorList {}
  25. expect {
  26. -re {Offending[[:space:]]+.*in[[:space:]]+([^:]+):([[:digit:]]+)} {
  27. lappend ErrorList [list $expect_out(1,string) $expect_out(2,string)]
  28. exp_continue
  29. }
  30. "Are you sure you want to continue connecting (yes/no)?" {
  31. exp_send "yes\r"
  32. exp_continue
  33. }
  34. "Please type 'yes' or 'no':" {
  35. exp_send "yes\r"
  36. exp_continue
  37. }
  38. -re {[pP]assword:} {
  39. puts "Set up ssh authorized_keys on remote for: ${user}@${host}"
  40. set rc 1
  41. }
  42. "Permission denied (publickey,keyboard-interactive)." {
  43. puts "Set up ssh authorized_keys on remote for: ${user}@${host}"
  44. set rc 1
  45. }
  46. -re "(No route to host|hostname nor servname provided, or not known|Host key verification failed)" {
  47. send_user "\nTerminating\r\n"
  48. puts "Failed to connect to: ${host}:${port}"
  49. set rc 1
  50. }
  51. -ex {$} {
  52. set rc 0
  53. }
  54. timeout {
  55. set rc 1
  56. }
  57. }
  58. if {0 == $rc} {
  59. exp_send "cd '${dir}' && byobu-launcher\r"
  60. expect {
  61. -ex "Menu:" {
  62. # send the power detach sequence
  63. exp_send "\1DD"
  64. }
  65. timeout {
  66. set rc 1
  67. }
  68. }
  69. }
  70. if {0 == $rc} {
  71. expect {
  72. "closed" {
  73. puts "screen started"
  74. }
  75. timeout {
  76. set rc 1
  77. }
  78. }
  79. }
  80. catch {
  81. close
  82. } ErrorMessage
  83. return $rc
  84. }
  85. # Main Program
  86. # ============
  87. set verbose 0
  88. set host ""
  89. set user $DefaultUser
  90. set port $DefaultPort
  91. set dir "."
  92. set i 0
  93. set arg [lindex $argv $i]
  94. while {$arg != ""} {
  95. switch -regexp -- $arg {
  96. "^--verbose" {
  97. set verbose 1
  98. }
  99. "^-v$" {
  100. set verbose 1
  101. }
  102. "^--cd=.+" {
  103. set pos [string first "=" $arg]
  104. set dir [string range $arg [expr $pos + 1] end]
  105. }
  106. "^--login=.+" {
  107. set pos [string first "=" $arg]
  108. set user [string range $arg [expr $pos + 1] end]
  109. }
  110. "^--user=.+" {
  111. set pos [string first "=" $arg]
  112. set user [string range $arg [expr $pos + 1] end]
  113. }
  114. "^[^:]+$" {
  115. set host $arg
  116. set port $DefaultPort
  117. }
  118. "^[^:]+:[^:]+$" {
  119. set pos [string first ":" $arg]
  120. set host [string range $arg 0 [expr $pos - 1]]
  121. set port [string range $arg [expr $pos + 1] end]
  122. }
  123. default {
  124. usage "Invalid argument $arg"
  125. }
  126. }
  127. incr i
  128. set arg [lindex $argv $i]
  129. }
  130. if {$user == ""} {
  131. usage "--login is not set"
  132. }
  133. if {$port == ""} {
  134. usage "port is not specified"
  135. }
  136. if {$host == ""} {
  137. usage "no host given"
  138. }
  139. if {[probe $user $host $port $dir] != 0} {
  140. usage "failed to start screen"
  141. }