您的位置:首页 > 运维架构 > Shell

Linux Bash expect 登录远程主机执行命令

2016-04-26 00:00 288 查看
摘要: centos6.4 expect 远程执行命令

第一篇博客,记录下前几天写的个远程执行命令的脚本,备忘

#! /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}"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: