在背景开发中,经常需要与人类进行交互的任务,例如登录到一个服务器,远程复制其上的软件并分发到其他服务器。
传统的操作方法会导致开发者与计算机的交互,例如等待用户输入密码,从而中断自动化过程的实现。
解决方案使用expect的内部命令spawn实现同样的自动化要求。
centos可以使用以下命令快速安装expect组件:
yum-y安装执行
情况1脚本1:test1.sh
实现功能:
远程登录到服务器并执行指定操作,例如:
、启动某项服务流程
、操作文件和文件夹
、scp操作;
#!/usr/xy dfj/expect settimeout 10 spawnsshroot @ 111.111.111.111 expect ' * password * ' send ' ABC 123r ' expect #。
实现功能:
、远程登录服务器
、在超时时间内(此处为60秒),等待用户在远程服务器上实现交互。
#!/usr/xy dfj/expect settimeout 60 spawn ssh-l root 111.111.111.111 expect ' password 3360 ' send ' ABC 123r ' interact机箱
实现功能:
、远程登录JumpServer
、将JumpServer上的文件拉伸到当前主机
transfer _ host=111.111.111.111 transfer _ ssh _ port=2233 transfer _ user=my _ admin transfer _ password=my pord pwd -P ) fuction remote_transfer ) echo ' $1' shift1expert exp _ continue ' ' password : ' { send ' ` echo $ transfer } r ' } } expecteofeof } remote _ transfer ' downloadfilesfromremotejumpservernow . ' scp-p $ { transfer _ ssh _ port }
第一次进行ssh,在一台服务器上,有以下提示,内部expect的第一行为自动填写是操作; $ssh-p2233111.111.111.111.111
theauthenticityofhost‘[ 111.111.111.111 ] :2233 ([ 111.111.111 ] :2233 ) can’tbe established。
ecdsakeyfingerprintissha 2563360 tsx r0d 8爱笑羊01 fcgideseqg0i6tz m3df 20。
ecdsakeyfingerprintismd 5: c 4: b 63360 b 0: da :313360 e 5: db 336020: Fe 3: d 0:3360 d 0333: d 0333333333: d
areyousureyouwanttocontinueconnecting (是/否)?
关于Linux base64加密的方法:
a )加密
$ echo密码| base64
cGFzc3dvcmQK
b ) .解密
$ echo cGFzc3dvcmQK|base64 -d
密码
引用列表
shell expect spawn,linux expect的使用方法记录linux expect spawn的使用方法