您现在的位置是:网站首页>列表内容
shell中使用expect命令进行远程执行命令脚本_linux shell_
2023-05-26 16:10:36 81人已围观
简介 shell中使用expect命令进行远程执行命令脚本_linux shell_
expect是用来实现自动交互功能的工具之一,使用expect-send来实现交互过程。
注意:
1、脚本的执行方法与bash shell不一样,比如:expect example.sh
2、向一个脚本传递参数时,bash shell是使用$1,$2...来接收参数的;而expect则将脚本的执行参数保存在数组$argv中,在脚本中一般将其赋值给变量:set 变量名 [lindex $argv 参数]
#!/usr/bin/expect set ip [lindex $argv 0] set password [lindex $argv 1] set timeout 2 spawn telnet $ip expect "*femto login:" send "root\r" expect "*Password:" send "$password\r" # 进入指定的机器后,就可执行相应的命令或者脚本 interact #expect eof
注意:若登陆后便退出远程终端,则写expect eof
即可。
3、执行脚本
expect autologin.sh 192.168.1.240 root
很多时候,需要用expect命令实现登录远端服务器执行简单命令,诸如:重启服务器,ftp,ls, scp等命令。 里面涉及到输入密码的交互式场景,这个时候expect命令的巨大功效就出来了,下面是一个比较经典脚本实现:
#!/usr/bin/tclsh package require Expect set host_ip1 [lindex $argv 0] set host_usr [lindex $argv 1] set host_pwd [lindex $argv 2] spawn ssh $host_usr@$host_ip1 set timeout 60 expect { -re "password" {send "$host_pwd\n"} -re "yes/no" {send "yes\n";exp_continue} # 有的时候输入几次密码来确认,exp_continue } expect "#" send "ls /home/${host_user} | tee -a /tmp/ls.txt \r" expect "#" send "exit\r" expect eof
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
点击排行
- 天天炫斗布鲁升级送钻石的发放介绍 布鲁升级送钻石发放时间_手机游戏_游戏攻略_
- 城堡争霸迷踪阵有什么用?什么时候用?_手机游戏_游戏攻略_
- 全民飞机大战风小象怎么样? 全民飞机大战小象属性技能介绍_手机游戏_游戏攻略_
- 全民打怪兽萌闯公测礼包获取方法 全民打怪兽萌闯公测礼包奖品奖励一览_手机游戏_游戏攻略_
- 合金弹头塔防怎么玩 合金弹头塔防简单玩法攻略推荐_手机游戏_游戏攻略_
- 全民打怪兽充值用户享五重豪礼活动火爆来袭 608钻石礼包轻松拿_手机游戏_游戏攻略_
- 暗黑战神新服永恒之井开启活动_手机游戏_游戏攻略_
- 钓鱼发烧友重制游戏密码操作流程介绍_手机游戏_游戏攻略_
- 迷你西游装备炼化小技巧_手机游戏_游戏攻略_
本栏推荐
-
天天炫斗布鲁升级送钻石的发放介绍 布鲁升级送钻石发放时间_手机游戏_游戏攻略_
-
城堡争霸迷踪阵有什么用?什么时候用?_手机游戏_游戏攻略_
-
全民飞机大战风小象怎么样? 全民飞机大战小象属性技能介绍_手机游戏_游戏攻略_
-
全民打怪兽萌闯公测礼包获取方法 全民打怪兽萌闯公测礼包奖品奖励一览_手机游戏_游戏攻略_
-
合金弹头塔防怎么玩 合金弹头塔防简单玩法攻略推荐_手机游戏_游戏攻略_
-
全民打怪兽充值用户享五重豪礼活动火爆来袭 608钻石礼包轻松拿_手机游戏_游戏攻略_
-
暗黑战神新服永恒之井开启活动_手机游戏_游戏攻略_
-
钓鱼发烧友重制游戏密码操作流程介绍_手机游戏_游戏攻略_
-
迷你西游装备炼化小技巧_手机游戏_游戏攻略_