expect 使用实例
自动登录一台 服务器
- 代码
[root@localhost D151SP160]# cat test1.exp
#!/bin/expect
set timeout 2set user_name [lindex $argv 0]
set mypassword [lindex $argv 1]
set ip [lindex $argv 2]#puts "user_name=$user_name"
#puts "user_password=$mypassword"
#puts "host_ip=$ip"
#
spawn ssh $user_name@$ip hostname
expect {
"continue" { send "yes\r"; exp_continue }
"*password:" { send "$mypassword\r" }
}
expect eof
- 执行
[root@localhost D151SP160]# /bin/expect ./test1.exp root admin_123 207.207.35.100
spawn ssh root@207.207.35.100 hostname
root@207.207.35.100's password:
node100
自动登录多台服务器
- test.exp
[root@localhost D151SP160]# cat test.exp
#!/bin/expect
set timeout 2set user_name [lindex $argv 0]
set mypassword [lindex $argv 1]
set ip [lindex $argv 2]#puts "user_name=$user_name"
#puts "user_password=$mypassword"
#puts "host_ip=$ip"
#
spawn ssh $user_name@$ip hostname
expect {
"continue" { send "yes\r"; exp_continue }
"*password:" { send "$mypassword\r" }
}
expect eof
- auto_login.sh
[root@node101 D151SP160]# cat auto_login.sh
#! /bin/sh
declare -A host_info
host_info[207.207.35.100]=admin_123
host_info[207.101.67.209]=admin_123for host in "${!host_info[@]}"
do#echo "host=$host"#echo "passwd=${host_info[$host]}"echo "--------------- begin to login in $host -----------"/bin/expect ./test.exp root ${host_info[$host]} $hostecho
done
- 执行
[root@node101 D151SP160]# sh auto_login.sh
--------------- begin to login in 207.207.35.100 -----------
spawn ssh root@207.207.35.100 hostname
root@207.207.35.100's password:
node100--------------- begin to login in 207.101.67.209 -----------
spawn ssh root@207.101.67.209 hostname
root@207.101.67.209's password:
node209
expect 使用实例相关推荐
- 常见的shell实例
1.从磁盘使用统计中排除部分文件 du --exclude "*.iso" dir 2.找出指定目录中最大的10个文件 find -type f -exec du -k {} \ ...
- 使用expect实现shell自动交互
shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...
- Linux expect与Shell交互
shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...
- expect返回值给shell_使用expect实现shell自动交互
shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...
- shell编程expect用法
shell脚本需要交互的地方可以使用here文档是实现,但是有些命令却需要用户手动去就交互如passwd.scp 对自动部署免去用户交互很痛苦,expect能很好的解决这类问题. expect的核心是 ...
- 前端开发基础知识汇总
一.HTML 1.前言与常用标签 浏览器 内核 备注 IE Trident IE.猎豹安全.360极速浏览器.百度浏览器 firefox Gecko 可惜这几年已经没落了,打开速度慢.升级频繁.猪一样 ...
- Spring Cloud微服务系统架构的一些简单介绍和使用
Spring Cloud 目录 特征 云原生应用程序 Spring Cloud上下文:应用程序上下文服务 引导应用程序上下文 应用程序上下文层次结构 改变Bootstrap的位置Properties ...
- expect学习笔记及实例详解【转】
1. expect是基于tcl演变而来的,所以很多语法和tcl类似,基本的语法如下所示: 1.1 首行加上/usr/bin/expect 1.2 spawn: 后面加上需要执行的shell命令,比如说 ...
- linux expect 通配符,Expect实例与expect使用简介
Expect可用于处理交互式程序:Expect is a program that "talks" to other interactive programs accordi ...
最新文章
- Queue接口及是实现类PriorityQueue介绍
- requests模拟登陆 验证码
- Nacos客户端配置
- 案例:返回浏览器需要的页面http服务器-代码实现
- SIP可靠性(SIP Reliability)
- 简约好看的域名售卖页html源码
- nyromodal 非常棒的弹出层,可内嵌各种文件
- 脊髓损伤有哪些方法方法?顾连康复方法有哪些
- android开发利用传感器实现微信的摇一摇功能(zz)
- oracle创建用户、创建表空间、授权、建表
- 随机变量的原点矩、中心距、变异系数
- MathType7新版本数学公式编辑器上线功能特性
- 会考计算机操作题知识点,江西省信息技术会考操作题知识要点汇总
- 服务器win2003系统升级,Windows server 2003 升级 到2008 R2
- 数值策划科普-------阈值
- erwfgergve
- 佛罗里达州的最新的野生动物保护区
- 解释源代码文件、目标代码文件和可执行文件之间的区别
- Linux 系统如何设置开机程序自启动
- java中的LinkedList(链表)与ArrayList(动态数组):(1)简单辨析