]> git.sven.stormbind.net Git - sven/scripts.git/blob - home/login.tcl
1d09e7e91c9b3cdf7acb89ed0dc4e5f8fd05897d
[sven/scripts.git] / home / login.tcl
1 #!/usr/bin/expect -f
2
3 set pwfile "/etc/openvpn/pass.txt"
4
5 # minimalistic argument parsing, only assumtion is that the first one will be the hostname
6 if { $argc == 1 } {
7     set host [lindex $argv 0]
8     set sshargs ""
9 } elseif { $argc > 1 } {
10     set host [lindex $argv 0]
11     set sshargs [lrange $argv 1 end]
12 } else {
13     puts "Error no hostname given"
14     puts "$argv0 hostname \[other ssh arguments\]"
15     exit 1
16 }
17
18 # extract username and password from an OpenVPN password file
19 # assumption is that the first line holds the username, the second the password
20 proc read_pwfile { pwfile } {
21  global user pass
22  set pwf [open $pwfile r]
23  set pwdat [read $pwf]
24  close $pwf
25
26  set pwlines [split $pwdat "\n"]
27  set user [lindex $pwlines 0]
28  set pass [lindex $pwlines 1]
29 }
30
31
32 read_pwfile $pwfile
33
34 # assemble the spawn command we plan to execute
35 if { [string length $sshargs] == 0 } {
36     set spawncmd "spawn ssh -l $user $host"
37 } else {
38     set spawncmd "spawn ssh -l $user $sshargs $host"
39 }
40
41 puts "Connecting with user $user to hosts $host with args $sshargs"
42 eval $spawncmd
43 expect -re ".*password:"
44 send "$pass\r"
45 interact