Skip to content

Instantly share code, notes, and snippets.

@wcomnisky
Last active January 31, 2017 01:36
Show Gist options
  • Save wcomnisky/4edccd9868c6ccfda1ab70755f8a28a2 to your computer and use it in GitHub Desktop.
Save wcomnisky/4edccd9868c6ccfda1ab70755f8a28a2 to your computer and use it in GitHub Desktop.
Expect script to SSH-access a machine, login to another user, run x11vnc and close the session after close the VNC client
#!/usr/bin/expect --
# Argument:
# argv0 = host
#
# Usage:
# expect.sh ip-or-hostname
spawn ssh -o StrictHostKeyChecking=no -p 22 -L 9000:localhost:5900 user@[lindex $argv 0]
set timeout 120
expect {
timeout {
puts "Connection timed out"
exit 1
}
"yes/no" {
send "yes\r"
exp_continue
}
"assword:" {
send -- "password-1\r"
expect "@hostname:"
}
}
send -- "su another-user\r"
expect "Password: "
send -- "password-2\r"
expect "another-user@hostname:"
send -- "DISPLAY=:0 x11vnc\r"
set viewerexited "false"
while {$viewerexited == "false"} {
sleep 1
expect {
"viewer exited." {
set viewerexited true
}
}
}
# Logout from 'user' user
send -- "exit\r"
expect "exit"
# ends SSH session
send -- "exit\r"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment