第一篇博客,记录下前几天写的个远程执行命令的脚本,备忘
#! /bin/bash
echo "Hello world"
:<<!
远程执行命令
!
declare -r IPADDR="192.168.197.206"
declare -r USERNAME="root"
declare -r PASSWD="123456"
declare -r COMMAND="ls -rlt"
function remoteExecCommand(){
local l_username="${1}"
local l_ipaddr="${2}"
local l_passwd="${3}"
local l_cmd="${4}"
expect << EOF
set timeout 10;
spawn ssh ${l_username}@${l_ipaddr};
expect {
"\[#$\]" {send "\r" }
"password:" {send "${l_passwd}\r"}
"(yes/no)?" {send "yes\r";exp_continue}
}
expect {
"\[#$\]" {send "\r" }
"Permission" {exit 3 }
}
expect "\[#$\]";send "${l_cmd}\r";
expect "\[#$\]";send "exit\r";
expect eof;
EOF
}
remoteExecCommand "${USERNAME}" "${IPADDR}" "${PASSWD}" "${COMMAND}"