functionmake_tmp_file()
{
local name=${1:-"rand"}
local magic=`date+%s`
echo"/tmp/__${magic}__${name}"
}
functionmake_rand_file()
{
make_tmp_file"$*"
}
# int exec_script(host, port, user, passwd, script, [timeout = 86400])
# 0, succ
# 1, fail
# 2, invalid params
# 3, fail
# 4, invalid script
# 5, login fail
# 6, put file fail
# 7, timeout
# 8, remote execute script fail
functionexec_script()
{
if[$# -lt 5 ]; then
return2
fi
local host=$1
local port=$2
local user=$3
local passwd=$4
local script=$5
local timeout=$((${6:-86400}))# default 1 day
local rscp=$(make_rand_file`basename$script`)
local ret=0
if[!-f$script];then
return4
fi
echo"script = ${script}, remote_script = ${rscp}"
ssh_put$host$port$user$passwd$script$rscp
expect-c"
set timeout$timeout
set flag0
spawn ssh$user@$host;
expect{
\"*assword*\"{send$passwd\r;}
\"yes\/no)?\"{
set flag1;
send yes\r;
}
\"Welcome\"{}
}
if{\$flag==1}{
expect{
\"*assword\"{
send$passwd\r;
}
}
}
expect{
\"*assword*\"{
puts\"INVALID PASSWD,host=$host,user=$user,passwd=$passwd\";
exit1
}
\"#\ \" {} \"$\ \" {} \">\ \" {}
}
send$rscp\r;
expect{
\"#\ \" {} \"$\ \" {} \">\ \" {}
}
send\"rm-f$rscp\r\"
send exit\r;
expecteof{
exit0
}
"
ret=$?
if[$ret-ne0];then
return5
fi
return0
}
# int exec_cmd(host, port, user, passwd, cmd, [timeout = 86400], [rfile = /dev/null])
# 0, succ
# 1, fail
# 2, invalid params
# 3, fail
# 4, invalid script
# 5, login fail
# 6, put file fail
# 7, timeout
# 8, remote execute script fail
functionexec_cmd()
{
if[$# -lt 5 ]; then
return4
fi
local host=$1
local port=$2
local user=$3
local passwd=$4
local cmd=$5
local timeout=$((${6:-86400}))# default 1 day
local rfile=${7-"/dev/null"}
#local log=$(make_tmp_file "${__GEAR_SH}_exec_cmd")
local ret=0
expect-c"
set timeout$timeout
set flag0
spawn ssh$user@$host;
expect{
\"*assword*\"{send$passwd\r;}
\"yes\/no)?\"{
set flag1;
send yes\r;
}
\"Welcome\"{}
}
if{\$flag==1}{
expect{
\"*assword\"{
send$passwd\r;
}
}
}
expect{
\"*assword*\"{
puts\"INVALID PASSWD,host=$host,user=$user,passwd=$passwd\";
exit1
}
\"#\ \" {} \"$\ \" {} \">\ \" {}
}
log_file${rfile}
send\"$cmd\r\";
expect{
\"#\ \" {} \"$\ \" {} \">\ \" {}
}
send exit\r;
expecteof{
exit0
}
"
cat${rfile}|sed-n'1,/[#$>].*exit/p'|sed'1d;$d'>${rfile}
#if [ -f ${log} ]; then rm ${log}; fi
ret=$?
if[$ret-ne0];then
return5
fi
return0
}
相关推荐
Shell编程之expect
今天小编就为大家分享一篇关于shell中使用expect命令进行远程执行命令脚本,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了详解Linux使用shell+expect远程登录主机的相关资料,这里提供了详细的实现步骤,需要的朋友可以参考下
linux script expect脚本编写方法
通过A服务器自动登录其他服务器自动传输文件、删除文件、自动检测tomcat启动状态,控制启动和停止、可配置多条服务器进行同步处理,适合集群管理
本篇文章给大家介绍在Linux中使用expect脚本实现远程机器自动登录,前提是要创建一个expec脚本ssh_expect,接下来定义一些命令别名等,下面跟着脚本之家小编一起学习学习吧
主要介绍了CentOS使用expect批量远程执行脚本和命令,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
《Shell脚本专家指南》旨在为Linux、Unix以及OSx系统管理员提供短小精悍且功能强大的shell实现解决方案,教会读者如何使用现有调试器调试shell脚本。全书分为3个部分:脚本技术基础、系统交互和高级技术、有用的脚本...
今天小编就为大家分享一篇关于Linux下使用expect命令编写自动化交互脚本,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
linux shell编程 Expect简介及应用中文教程
linux下的expect的简单用法及举例
正常发送文件或自动化登录远程机器,需要与机器互动。...使用它系统管理员的可以创建脚本用来实现对命令或程序提供输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。
关于自动化脚本expect的一个例子。本脚本功能是 实现自动输入密码登录到一个server机器上。
linux下交互式命令expect使用方法
主要介绍了Shell脚本实现自动输入密码登录服务器,本文使用expect来实现这个需求,讲解了expect的安装及使用脚本,需要的朋友可以参考下
expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信
shell脚本无密码登录 expect的使用方法详解 今天需要做一个定时任务脚本将最新的数据包文件传到远程的服务器上,虽然有密钥但也是要求输入密码的那种,所以只能另想办法实现让脚本自动输入密码了。 从网上查到使用...
Linux运维-运维系统服务04-Shell脚本d5-Shell随机数与循环嵌套-12expect程序交互2.mp4