Created
August 23, 2013 16:57
-
-
Save ksafranski/6321574 to your computer and use it in GitHub Desktop.
Update user example script for Tcl-Expect presentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/expect | |
set timeout 3 | |
log_user 0 | |
# Define some variables | |
set user_prefix "user" | |
set server "echosec.com" | |
set num_users 3 | |
# Get arguments | |
set user [ lindex $argv 0 ] | |
set password [ lindex $argv 1 ] | |
set updates [ lindex $argv 2 ] | |
# SSH into server | |
puts "Opening SSH Connection\n" | |
spawn ssh "$user\@$server" | |
# Expecting password or key prompt(s) | |
expect { | |
"assword" { | |
send "$password\r" | |
} | |
"yes/no" { | |
send "yes\r" | |
} | |
} | |
# Expect prompt | |
expect "$" | |
# Function for updating user(s) | |
proc run_update { cur } { | |
puts "Updating $cur\n" | |
send "su $cur\r" | |
expect "$" | |
send "cd ~/www-data\r" | |
expect "$" | |
send "git pull origin master\r" | |
expect "$" | |
send "exit\r" | |
} | |
# Update all, or just one? | |
if { $updates == "all" } { | |
# Loop through all the users | |
while {$num_users > 0 } { | |
set updating [ run_update $user_prefix$num_users ] | |
set num_users [expr $num_users-1] | |
} | |
} else { | |
# Just update the supplied user | |
set updating [ run_update $user_prefix$updates ] | |
} | |
expect "$" | |
send "exit\r" | |
interact |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment