上传文件时经常要输入密码所以搜了下expect的用法
expect 模式
expect在shell脚本中有两种运行方式,一种是指定解释器.当然也可以执行shell
#!/usr/bin/expect
exec sh -c {shell commond}
spawn 需要输入密码的shell命令
expect “assword”
send “pwrd\r”
interact- 一种是 expect -c “要执行的命令”
- 这两种方式推荐第二种,因为expect不支持shell语法很多shell功能没法用
scp命令
···
source_file=$@
expect -c ”
spawn scp -r $source_file
expect {
\”assword\” {set timeout -1; send \”pwrd\r\”; exp_continue; }
}
“
“`
ssh 远程执行sh脚本后退出登录
call_sh=$1
echo -e “start call \033[40;31m $call_sh \033[0m”
expect -c ”
spawn ssh -l root 127.0.0.1
expect {
\”*assword:\” {set timeout -1; send \”pwrd\r\”; exp_continue; }
# 此处要指定远程脚本的解释器
\”$\” {set timeout -1; send \”expect ${call_sh}\r\”; }
}
expect \”$\”
send \”exit\r\”
interact
exit
“
本文介绍如何利用Expect脚本简化SSH登录及SCP文件传输过程中的密码输入步骤。通过expect-c命令行方式执行SSH远程命令及SCP命令,并在脚本中自动处理密码提示,实现自动化操作。
907

被折叠的 条评论
为什么被折叠?



