Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 165 lines (141 sloc) 3.167 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#!/bin/sh
# -*- tcl -*- \
exec expect -f "$0" "$@"


set DefaultUser wr
set DefaultPort 22
set timeout 120


proc usage {arg} {
  global argv0 DefaultUser DefaultPort
  if {$arg != ""} {
    puts "error: ${arg}"
  }
  puts "usage: ${argv0} {--verbose} {--cd=dir} {--login=user} host{:port}"
  puts " default user = ${DefaultUser}"
  puts " default port = ${DefaultPort}"
  exit 1
}

if {[llength $argv] <= 0} {
  usage "missing arguments"
}


# assumes the command will execute non-interactively
proc probe {user host port dir} {

  spawn ssh -l $user -p $port $host

  set rc 0
  set ErrorList {}

  expect {
    -re {Offending[[:space:]]+.*in[[:space:]]+([^:]+):([[:digit:]]+)} {
      lappend ErrorList [list $expect_out(1,string) $expect_out(2,string)]
      exp_continue
    }
    "Are you sure you want to continue connecting (yes/no)?" {
      exp_send "yes\r"
      exp_continue
    }
    "Please type 'yes' or 'no':" {
      exp_send "yes\r"
      exp_continue
    }
    -re {[pP]assword:} {
      puts "Set up ssh authorized_keys on remote for: ${user}@${host}"
      set rc 1
    }
    "Permission denied (publickey,keyboard-interactive)." {
      puts "Set up ssh authorized_keys on remote for: ${user}@${host}"
      set rc 1
    }
    -re "(No route to host|hostname nor servname provided, or not known|Host key verification failed)" {
      send_user "\nTerminating\r\n"
      puts "Failed to connect to: ${host}:${port}"
      set rc 1
    }
    -ex {$} {
      set rc 0
    }
    timeout {
      set rc 1
    }
  }

  if {0 == $rc} {
    exp_send "cd '${dir}' && byobu-launcher\r"
    expect {
      -ex "Menu:" {
        # send the power detach sequence
        exp_send "\1DD"
      }
      timeout {
        set rc 1
      }
    }
  }

  if {0 == $rc} {
    expect {
      "closed" {
        puts "screen started"
      }
      timeout {
        set rc 1
      }
    }
  }

  catch {
    close
  } ErrorMessage

  return $rc
}


# Main Program
# ============

set verbose 0
set host ""
set user $DefaultUser
set port $DefaultPort
set dir "."

set i 0
set arg [lindex $argv $i]
while {$arg != ""} {
  switch -regexp -- $arg {
    "^--verbose" {
      set verbose 1
    }
    "^-v$" {
      set verbose 1
    }
    "^--cd=.+" {
      set pos [string first "=" $arg]
      set dir [string range $arg [expr $pos + 1] end]
    }
    "^--login=.+" {
      set pos [string first "=" $arg]
      set user [string range $arg [expr $pos + 1] end]
    }
    "^--user=.+" {
      set pos [string first "=" $arg]
      set user [string range $arg [expr $pos + 1] end]
    }
    "^[^:]+$" {
      set host $arg
      set port $DefaultPort
    }
    "^[^:]+:[^:]+$" {
      set pos [string first ":" $arg]
      set host [string range $arg 0 [expr $pos - 1]]
      set port [string range $arg [expr $pos + 1] end]
    }
    default {
      usage "Invalid argument $arg"
    }
  }
  incr i
  set arg [lindex $argv $i]
}

if {$user == ""} {
  usage "--login is not set"
}

if {$port == ""} {
  usage "port is not specified"
}

if {$host == ""} {
  usage "no host given"
}


if {[probe $user $host $port $dir] != 0} {
  usage "failed to start screen"
}
Something went wrong with that request. Please try again.