简单模式:

#!/usr/bin/expect -f
set timeout
spawn ssh root@192.168.0.1
expect "*assword*"
send "root\r"
expect "#"
send "ifconfig \r"
expect eof

讲解:

send:用于向进程发送字符串
expect:从进程接收字符串 比如:expect "*assword*"

spawn:启动新的进程
interact:允许用户交互

怎么使用变量:
#!/usr/bin/expect -f
set port
set user root
set host 192.168.0.12
set password root
set timeout -
spawn ssh -D $port $user@$host "ifconfig"
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
expect "*#*"
send "ifconfig > /home/cfg \r"
send "exit\r"
}

讲解:

expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
选择模式,exp_continue表示继续。 通过读取配置文件获取变量:
配置文件
192.168.0.1 root
192.168.0.2 root

自动化登录脚本

#!/usr/bin/expect -f
set f [open ./ip r]
while { [gets $f line ]>= } {
set ip [lindex $line ]
set pwd [lindex $line ]
spawn ssh $ip
expect "*password:" { send "$pwd\r" }
expect "#"
send "ifconfig \r"
send "exit\r"
interact
}

讲解:

可以多台服务器循环执行,是个非常使用的方式!

自动化远程拷贝文件:

#!/usr/bin/expect -f
set port
set user root
set host 192.168.28.30
set password root
set timeout -
spawn scp $host:/home/cfg ./
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
expect eof

讲解:

原理和ssh一样

远程执行命令后写入文件,再通过scp到本机服务器:

#!/usr/bin/expect -f

set port
set user root
set host 192.168.28.30
set password root
set timeout -
spawn ssh -D $port $user@$host "ifconfig"
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
expect "*#*"
send "ifconfig > /home/cfg \r"
send "exit\r"
interact
spawn scp $host:/home/cfg ./
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
expect eof

讲解:

自动化运维,远程交互从服务器A上ssh到服务器B上,然后执行服务器B上的命令。

http://www.cnblogs.com/Javame/p/4272440.html

Linux - expect自动化远程登录脚本的更多相关文章

  1. Linux Expect自动化交互脚本简介

    相关资料 维基百科:Expect SourceForge:The Expect Home Page TCL脚本言语简介 由于Expect是建立在TCL语言基础上的一个工具,因此首先检查一些TCL常见语 ...

  2. expect脚本远程登录、远程执行命令和脚本传参简单用法

    expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...

  3. 每次进步一点点——linux expect 使用

    1. 介绍 expect是建立在tcl(参见:Tcl/Tk快速入门 )基础上的一个工具,它可以让一些需要交互的任务自动化地完成.相当于模拟了用户和命令行的交互操作. 一个具体的场景:远程登陆服务器,并 ...

  4. [转载]expect spawn、linux expect 用法小记

    原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...

  5. Linux expect自动登录ssh,ftp

    [http://blog.51yip.com/linux/1462.html#] #!/usr/bin/expect -f set ip 192.168.1.201 set password meim ...

  6. Linux 下 SSH 远程超时解决方案

    Linux 下 SSH 远程超时解决方案 今天突然看到一个问题说是如何解决SSH远程超时的问题. 找了一点资料.用于解决这个需求~ 第一种:OpenSSH 配置文件设置 位于112行的 "C ...

  7. Shell编程之Expect自动化交互程序

    一.Expect自动化交互程序 1.spawn命令 通过spawn执行一个命令或程序,之后所有的Expect操作都会在这个执行过的命令或程序进程中进行,包括自动交互功能. 语法: spawn [ 选项 ...

  8. linux的“自动化”

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  9. linux expect, spawn用法小记

    linux expect, spawn用法小记_IT民工_百度空间 linux expect, spawn用法小记 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://sys ...

随机推荐

  1. angularjs 请求后端接口请求了两次

    用angularjs的过程中发现,每次打开页面,请求后端的接口都请求了两次 如下图可以看到, http://192.168.1.109:8080/zdh/api/v1/goods/54 这个页面loa ...

  2. (六)WebGIS中地图瓦片在Canvas上的拼接显示原理

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 在之前的五个章节中,我们在第一章节里介绍了WebGIS的基本 ...

  3. JS查找数组中出现的位置及个数

    查找某个值在数组中出现的位置 var attr = [1,4,5,3,2,7,6,9]; var zhao = 8; var sy = -1; for(var i=0;i<attr.length ...

  4. 纯css3艺术文字样式效果代码

    效果:http://hovertree.com/texiao/css3/1/ 本效果主要使用text-shadow实现.参考:http://hovertree.com/h/bjaf/css3_text ...

  5. sqlHelper做增删改查

    1.把数据库里面的数据显示出来 sqlHelper怎么用:[网上可以下载,需要可以找楼主要] 1.拷贝到项目,修改它的命名空间等于当前项目名称 2.数据库的连接信息,用户名,密码,登录方式等 < ...

  6. 转载:《TypeScript 中文入门教程》 17、注解

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 介绍 随着TypeScript和ES6里引入了类,现在在一些场景下我们会需要额外的特性 ...

  7. No.026:Remove Duplicates from Sorted Array

    问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. JAVA 入门第一章(语法基础)

    本人初学java 博客分享记录一下自己的学习历程 java我的初步学习分为六章,有c和c++的基础学起来也简便了很多. 第一章 语法基础 第二章 面向对象 第三章 常用工具类 第四章 文件操纵 第五章 ...

  9. GJM : 各大开发游戏引擎

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  10. ASP对XML的增、删、改、查

    首先看一下xml文件 text.xml 'encoding使用gb2312中文,如果要用英文则用utf-8 <?xml version="1.0" encoding=&quo ...