File: //scripts.20110531.215904.25158/usersshcmd
#!/usr/bin/expect --
set host [lindex $argv 0]
set user [lindex $argv 1]
set cmd [lrange $argv 2 99]
set passes [read stdin]
set pass [lindex $passes 0]
set rppass [lindex $passes 1]
if {[string length $host] == 0} {
puts "You must set a host"
exit
}
if {[string length $user] == 0} {
puts "You must set a user"
exit
}
if {[string length $cmd] == 0} {
puts "You must set a cmd"
exit
}
if {[string length $passes] == 0} {
puts "You must set a passes"
exit
}
spawn ssh -o "StrictHostKeyChecking no" $user@$host
set timeout 90
expect {
"*assword:" {
sleep 1;
send "$pass\r"
}
"*REMOTE HOST IDENTIFICATION*" {
puts "\n\nsshhostproblem\n"
puts "\n\nsshcmddone\n"
exit
}
"*Name or service not known*" {
puts "\n\nsshhostnotfound\n"
puts "\n\nsshcmddone\n"
exit
}
"*Connection timed out*" {
puts "\n\nsshconnecttimeout\n"
puts "\n\nsshcmddone\n"
exit
}
"*Connection closed*" {
puts "\n\nsshdisconnectproblem\n"
puts "\n\nsshcmddone\n"
exit
}
"*No route to host*" {
puts "\n\nsshdisconnectproblem\n"
puts "\n\nsshcmddone\n"
exit
}
"*Connection refused*" {
puts "\n\nsshdisconnectproblem\n"
puts "\n\nsshcmddone\n"
exit
}
}
set timeout 90
expect {
"Permission denied, please try again*" {
puts "\n\nsshcmdpermissiondeny\n"
puts "\n\nsshcmddone\n"
exit
}
expect "*\$*" {
send "su\r"
}
expect "*$*" {
send "su\r"
}
expect "*]*" {
send "su\r"
}
expect "*#*" {
send "su\r"
}
expect "*$ *" {
send "su\r"
}
expect "*] *" {
send "su\r"
}
"*Connection timed out*" {
puts "\n\nsshconnecttimeout\n"
exit
}
}
expect -timeout 90 "*assword:" {
sleep 1;
send "$rppass\r"
}
set timeout 15
expect {
"*incorrect*" {
puts "\n\nsshcmdpermissiondeny\n"
puts "\n\nsshcmddone\n"
exit
}
expect "*#*" {
send "(echo sshcmdstart;$cmd;echo sshcmddone)\r"
}
expect "*# *" {
send "(echo sshcmdstart;$cmd;echo sshcmddone)\r"
}
expect "*]*" {
send "(echo sshcmdstart;$cmd;echo sshcmddone)\r"
}
expect "*] *" {
send "(echo sshcmdstart;$cmd;echo sshcmddone)\r"
}
}
set timeout 3600
expect {
"\nsshcmddone" {
}
}
expect {
expect "*] *" {
sleep 1
send "exit\r"
send "exit\r"
}
expect "*# *" {
sleep 1
send "exit\r"
send "exit\r"
}
expect "*]*" {
sleep 1
send "exit\r"
send "exit\r"
}
expect "*#*" {
sleep 1
send "exit\r"
send "exit\r"
}
}