#!/bin/sh
exec expect -- "$0" ${1+"$@"}
set port 22
set user "root"
set timeout 120
set password ""
set host ""
set mode ""
set command ""
set src ""
set dst ""
set key "$env(g_aes_rand_key)"
proc help {} {
global argv0
send_user "usage: $argv0\n"
send_user " -i <ip> Host or IP\n"
send_user " -P <port> Port. Default = 22\n"
send_user " -u <user> UserName. Default = root\n"
send_user " -p <password> Password.\n"
send_user " -t <timeout> Timeout. Default = 120\n"
send_user " -m <mode> Mode. include: ssh-cmd, scp-out, scp-in\n"
send_user " -c <command> Ssh Command\n"
send_user " -s <src> Scp Source File\n"
send_user " -d <dst> Scp Destination File\n"
send_user " -a <aes-file> Use aes encrypt passwd\n"
send_user " -v Version\n"
send_user " -h Help\n"
send_user "Sample:\n"
send_user "$argv0 -i 0.0.0.0 -p pass -t 5 -m ssh-cmd -c ifconfig\n"
send_user "$argv0 -i 0.0.0.0 -p pass -m scp-out -s /etc/passwd -d /tmp/passwd\n"
}
proc errlog {errmsg h code} {
global host
send_user "Error: $errmsg on $host (${code}) \n"
if {[string compare "$h" "yes"] == 0} {
help
}
exit $code
}
if {[llength $argv] == 0} {
errlog "argv is null" "yes" "1"
}
while {[llength $argv]>0} {
set flag [lindex $argv 0]
switch -- $flag "-i" {
set host [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-P" {
set port [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-u" {
set user [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-p" {
set password [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-t" {
set timeout [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-m" {
set mode [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-c" {
set command [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-s" {
set src [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-d" {
set dst [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-a" {
set password [ exec openssl enc -aes-256-cbc -salt -a -d -k "$key" -in [lindex $argv 1] 2> /dev/null ]
set argv [lrange $argv 2 end]
} "-v" {
send_user "Ver: 1.0\n"
exit 0
} "-h" {
help
exit 0
} default {
set user [lindex $argv 0]
set argv [lrange $argv 1 end]
break
}
}
if {"$host" == ""} {
errlog "host is null" "yes" "1"
}
if {[string compare "$mode" "ssh-cmd"] == 0} {
if {"$command" == ""} {
errlog "command is null" "yes" "1"
}
spawn ssh -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -t -p $port $user@$host "$command"
} elseif {[string compare "$mode" "scp-out"] == 0} {
if {"$src" == "" || "$dst" == ""} {
errlog "src or dst is null" "yes" "1"
}
spawn scp -r -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -P $port $src $user@$host:$dst
} elseif {[string compare "$mode" "scp-in"] == 0} {
if {"$src" == "" || "$dst" == ""} {
errlog "src or dst is null" "yes" "1"
}
spawn scp -r -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -P $port $user@$host:$src $dst
} else {
errlog "mode($mode) invalid" "yes" "1"
}
expect {
-nocase -re "please try again" {
errlog "Bad Password/UserName, Or Account locked" "no" "128"
}
-nocase -re "password" {
send "$password\r"
exp_continue
}
timeout {
errlog "Executing timeout" "no" "129"
}
}
catch wait result
set ret [lindex $result 3]
if { $ret != 0 } {
if {$ret == 1 && [string compare "$mode" "scp-out"] == 0} {
spawn /bin/sh -c "cat $src | ssh -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -t -p $port $user@$host 'cat > $dst'"
expect {
-nocase -re "please try again" {
errlog "Bad Password/UserName, Or Account locked" "no" "128"
}
-nocase -re "password" {
send "$password\r"
exp_continue
}
timeout {
errlog "Executing timeout" "no" "129"
}
}
catch wait result
set ret [lindex $result 3]
if { $ret == 0 } {
exit 0
}
}
errlog "Execute failed" "no" "$ret"
}
exit $ret