sshw (1843B)
1 #!/usr/bin/expect 2 set timeout -1 3 match_max 100000 4 5 if { $argc < 1 } { 6 send_user "No hostname provided\n" 7 exit 1 8 } 9 10 set command "ssh" 11 for {set i 0} {$i < $argc} {incr i} { 12 switch -glob [lindex $argv $i] { 13 -J { 14 lappend command "-J" [lindex $argv [expr {$i + 1}]] 15 incr i 16 } 17 -R { 18 lappend command "-R" [lindex $argv [expr {$i + 1}]] 19 incr i 20 } 21 -L { 22 lappend command "-R" [lindex $argv [expr {$i + 1}]] 23 incr i 24 } 25 default { 26 lappend command [lindex $argv $i] 27 } 28 } 29 } 30 31 # Define a subroutine to read password 32 proc read_password {} { 33 stty -echo 34 send_user "password: " 35 expect_user -re "(.*)\n" 36 stty echo 37 set pass "$expect_out(1,string)\r" 38 send_user "\n" 39 return $pass 40 } 41 42 # Read password from user 43 set pass [read_password] 44 45 # Define procedure to log in 46 proc login {pass command} { 47 # Variables written in procedures don't change globally, 48 # but spawn_id has to be global for expect to work. 49 global spawn_id 50 51 # Connect 52 eval spawn $command 53 54 expect { 55 # Change password as needed 56 -ex "New password: " { 57 send -- "$pass" 58 expect -exact "Retype new password: " 59 send -- "$pass" 60 expect eof 61 62 # SSH closes the connection, so log in again 63 login $pass $command 64 } 65 # Accept fingerprint as needed 66 -ex "Are you sure you want to continue connecting (yes/no/\[fingerprint\])? " { 67 send -- "yes\r" 68 exp_continue 69 } 70 # On a shell prompt, stop expecting 71 " $" {} 72 # Catch all - we got something we didn't expect 73 default { 74 send_user "Got some unexpected output\n" 75 exit 1 76 } 77 } 78 } 79 80 # Log in 81 login $pass $command 82 83 # Elevate privileges 84 send -- "sudo su\n" 85 expect -re "password for \[^ \]+: " 86 send -- "$pass" 87 88 if { $argc == 2 } { 89 send -- "[lindex $argv 1]\n" 90 } 91 # Yield control 92 interact