案例

例1:
从本机自动登录到远程机器192.168.1.200(端口是22,密码是:PASSWORD)
登录到远程机器后做以下几个操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit自动退出

  1. [root@xw4 tmp]# cat test-ssh.sh
  2. #!/bin/bash
  3. passwd='PASSWORD'
  4. /usr/local/bin/expect <<-EOF
  5. set time 30
  6. spawn ssh -p22 root@192.168.1.201
  7. expect {
  8. "*yes/no" { send "yes\r"; exp_continue }
  9. "*password:" { send "$passwd\r" }
  10. }
  11. expect "*#"
  12. send "useradd wangshibo\r"
  13. expect "*#"
  14. send "mkdir /opt/test\r"
  15. expect "*#"
  16. send "exit\r"
  17. interact
  18. expect eof
  19. EOF
  20.  
  21. [root@xw4 tmp]# sh test.sh
  22. spawn ssh -p22 root@192.168.1.201
  23. root@192.168.1.201's password:
  24. Last login: Fri Sep 23 16:21:20 2016 from 192.168.1.23
  25. [root@vm-002 ~]# useradd wangshibo
  26. [root@vm-002 ~]# mkdir /opt/test
  27. [root@vm-002 ~]# [root@xw4 tmp]#

例2:我们在部署无密码访问时,手工建立ssh互信需要好几个步骤,并且中途人工交互(输入密码等),如果机器数目多,则很繁琐!
下面方法用于自动化生成authorized_keys,免去了手工数据.
方法: 利用expect编写sshkey.exp在远程主机上生成id_rsa,并重定向到本地.在利用noscp.exp.把文件复制到远程主机
为了节省自己的时间,可以写个expect自动化脚本,分享如下:

  1. 1
  2. 如上expect安装后的路径是:
  3. [root@xw4 ~]# which expect
  4. /usr/local/bin/expect
  5.  
  6. 2
  7. 做个expect执行文件的软件
  8. [root@xw4 ~]# ln -s /usr/local/bin/expect /usr/bin/expect
  9. [root@xw4 ~]# ll /usr/bin/expect
  10.  
  11. 3
  12. 编写expect脚本:
  13. -----------------------------------------------------------------------------------
  14. 1
  15. [root@xw4 ~]# cat sshkey.exp
  16. #!/usr/bin/expect
  17.  
  18. #sshkey.exp
  19.  
  20. if {$argc<3} {
  21. puts stderr "Usage: $argv0 host user passwd "
  22. exit 1
  23. }
  24.  
  25. set host [ lindex $argv 0 ]
  26. set user [ lindex $argv 1 ]
  27. set pwd [ lindex $argv 2 ]
  28.  
  29. set timeout 30
  30.  
  31. #spawn ssh ${user}@${host} "rm -rf ~/.ssh/id_rsa*"
  32. #
  33. #expect {
  34. # "*yes/no" { send "yes\r"; exp_continue }
  35. # "*password:" { send "$pwd\r"; exp_continue }
  36. #}
  37.  
  38. spawn ssh ${user}@${host} "ssh-keygen -t rsa"
  39.  
  40. expect {
  41. "*yes/no" { send "yes\r"; exp_continue }
  42. "*password:" { send "$pwd\r"; exp_continue }
  43. "Enter file in which to save the key*" { send "\n\r"; exp_continue }
  44. "Overwrite*" { send "y\n"; exp_continue }
  45. "Enter passphrase (empty for no passphrase):" { send "\n\r"; exp_continue }
  46. "Enter same passphrase again:" { send "\n\r" }
  47. }
  48.  
  49. spawn ssh ${user}@${host} "cat ~/.ssh/id_rsa.pub"
  50.  
  51. expect {
  52. "*yes/no" { send "yes\r"; exp_continue }
  53. "*password:" { send "$pwd\r" }
  54. }
  55.  
  56. expect eof
  57.  
  58. ----------------------------------------------------------------------------------------------------
  59. 2
  60. [root@xw4 ~]# cat noscp.exp
  61. #!/usr/bin/expect
  62.  
  63. #noscp.exp
  64.  
  65. if {$argc<4} {
  66. puts stderr "Usage: $argv0 localfile remotefile user passwd "
  67. exit 1
  68. }
  69.  
  70. set localfile [ lindex $argv 0 ]
  71. set remotefile [ lindex $argv 1 ]
  72. set user [ lindex $argv 2 ]
  73. set pwd [ lindex $argv 3 ]
  74.  
  75. set timeout 30
  76.  
  77. spawn scp ${localfile} ${user}@${remotefile}
  78.  
  79. expect {
  80. "*yes/no" { send "yes\r"; exp_continue }
  81. "*password:" { send "$pwd\r" }
  82. }
  83.  
  84. expect eof
  85.  
  86. ------------------------------------------------------------------------
  87.  
  88. [root@xw4 ~]# chmod 755 sshkey.exp
  89. [root@xw4 ~]# chmod 755 noscp.exp
  90.  
  91. 4
  92. 脚本说明
  93. ./sshkey.exp 主机名 用户名 密码 (在远程主机生成id_rsa)
  94. ./noscp.exp 本地文件 远程路径 远程用户密码 (无密码拷贝文件)
  95.  
  96. 5)验证:
  97. [root@xw4 ~]# ./sshkey.exp 192.168.1.201 root PASSWORD |grep ssh-rsa >> ~/.ssh/authorized_keys
  98. [root@xw4 ~]# ./noscp.exp ~/.ssh/authorized_keys 192.168.1.201:~/.ssh root PASSWORD
  99. spawn scp /root/.ssh/authorized_keys root@192.168.1.201:~/.ssh
  100. root@192.168.1.201's password:
  101. authorized_keys
  102.  
  103. 这样,就能无密码登陆了!
  104. [root@xw4 ~]# ssh 192.168.1.201
  105. Last login: Fri Sep 23 18:33:21 2016 from 192.168.1.7
  106. [root@vm-002 ~]#
  107.  
  108. --------------------------------------------------------------------------
  109. 如果是多台机器的话,可以结合shell脚本进行批量执行
  110.  
  111. [root@xw4 ~]# cat /root/ip.list
  112. 192.168.1.100
  113. 192.168.1.101
  114. 192.168.1.102
  115. 192.168.1.103
  116. 192.168.1.104
  117. ......
  118. ......
  119.  
  120. [root@xw4 ~]# cat sshkey.sh
  121. #!/bin/bash
  122. user='root'
  123. password='PASSWORD'
  124. for ip in `cat /root/ip.list`
  125. do
  126. /root/sshkey.exp $ip $user $password |grep ssh-rsa >> ~/.ssh/authorized_keys
  127. /root/noscp.exp ~/.ssh/authorized_keys $user@$ip:~/.ssh root PASSWORD
  128. done

其他

dispatch_copy.exp

  1. #! /usr/local/bin/expect
  2. if { $argc != 3 } {
  3. send_user "usage :expect dispatch_sshkey.expect file host dest \n"
  4. }
  5.  
  6. #define var
  7. set timeout 3
  8. set file [lindex $argv 0]
  9. set host [lindex $argv 1]
  10. set dest [lindex $argv 2]
  11. set password "hhh"
  12. #spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
  13. #spawn scp -P11544 $file root@$host:$dir
  14. #spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
  15. #spawn ssh-copy-id -i .ssh/id_dsa.pub omd@192.168.25.137
  16. spawn scp $file oldgirl@$host:$dest
  17. expect of {
  18. #timeout 1
  19. "yes/no" {send "yes\r"}
  20. "*password" {send "$password\r"}
  21. timeout {puts "Expect was timeout please contact ftl at XXXXX"; return}
  22. }
  23.  
  24. exit -onexit {
  25. send_user "good bye!! see you \n"
  26. }

dispatch-sshkey.exp

  1. #! /usr/local/bin/expect
  2. if { $argc !=2 } {
  3. send_user "usage :expect dispatch_sshkey.expect file host \n"
  4. exit
  5. }
  6. #define var
  7. set file [lindex $argv 0]
  8. set host [lindex $argv 1]
  9. set password "hhh"
  10. #spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
  11. #spawn scp -P11544 $file root@$host:$dir
  12. #spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
  13. #spawn ssh-copy-id -i .ssh/id_dsa.pub omd@192.168.25.137
  14. spawn ssh-copy-id -i $file oldgirl@$host
  15. expect {
  16. "yes/no" {send "yes\r";exp_continue}
  17. "*password" {send "$password\r"}
  18. }
  19. expect eof
  1.  

dispatch-sshkey2.exp[更完善]

  1. #! /usr/local/bin/expect
  2. if { $argc !=2 } {
  3. send_user "usage :expect dispatch_sshkey.expect file host \n"
  4. exit
  5. }
  6. #define var
  7. set timeout 1
  8. set file [lindex $argv 0]
  9. set host [lindex $argv 1]
  10. set password "hhh"
  11. #spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
  12. #spawn scp -P11544 $file root@$host:$dir
  13. #spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
  14. #spawn ssh-copy-id -i .ssh/id_dsa.pub omd@192.168.25.137
  15. spawn ssh-copy-id -i $file oldgirl@$host
  16. expect {
  17. #timeout 1
  18. "yes/no" {send "yes\r";exp_continue}
  19. "*password" {send "$password\r"}
  20. timeout {puts "Expect was timeout "; return}
  21. }
  22. expect eof
  23. exit -onexit {
  24. exec rm $tmpfile
  25. send_user "good bye\n"
  26. }

Linxu下 expect的实用实例_1的更多相关文章

  1. Linxu下 expect的安装与使用

    expect学习 1.什么是except        Expect是基于Tcl的一个相对简单的免费脚本文件语言工具,用于实现自动和交互式程序进行通信            is a software ...

  2. jQuery UI 入门之实用实例分享

    jQuery UI 入门 jQuery UI 简介 jQuery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,您可以使用它创建高度交互的 Web 应用程序.无论您是创 ...

  3. jQuery UI 入门之实用实例

    jQuery UI 入门 jQuery UI 简介 jQuery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,您可以使用它创建高度交互的 Web 应用程序.无论您是创 ...

  4. linux下expect环境安装以及简单脚本测试

    expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装 下 ...

  5. Ubuntu下三个实用的录屏软件

    Ubuntu下三个实用的录屏软件 Kazam 优点: 易安装 可选择区域录制,也可全屏录制 有录屏和截图功能 安装: sudo apt-get install kazam 展示: Simple Scr ...

  6. Linux 下 expect 脚本语言中交互处理常用命令

    Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...

  7. expect基础及实例

    expect基础及实例 http://blog.csdn.net/zhuying_linux/article/details/6900805

  8. Linxu下Yii2的POST请求被拒经历

    Linxu下Yii2的POST提交被拒经历 介于对Yii2的使用,浅谈一下自己的经验,在以往的项目中我使用的框架是Yii1,由于Yii2的出现,所以极力的想使用一下它的新特性. 我的使用环境Linux ...

  9. Linux下 expect 使用详解与实例

    一.概述 我们通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet服务器等进行交互的功能.而Expec ...

随机推荐

  1. 033-JsonUtils 工具类模板

    模板一:使用的是jackson package cn.e3mall.common.utils; import java.util.List; import com.fasterxml.jackson. ...

  2. 【LDAP】LDAP常用命令解析

    ldapadd -x   进行简单认证-D   用来绑定服务器的DN-h   目录服务的地址-w   绑定DN的密码-f   使用ldif文件进行条目添加的文件例子 ldapadd -x -D &qu ...

  3. [中英对照]Device Drivers in User Space: A Case for Network Device Driver | 用户态设备驱动: 以网卡驱动为例

    前文初步介绍了Linux用户态设备驱动,本文将介绍一个典型的案例.Again, 如对Linux用户态设备驱动程序开发感兴趣,请阅读本文,否则请飘过. Device Drivers in User Sp ...

  4. Intellij Idea快捷鍵

    一.视图查看 Ctrl+F12 查看file,method结构图.类继承机构图 (不知道方法结构,Ctrl+F12一下,方法,参数,返回值,一清二楚的展现出来) Ctrl+shift+Alt+U   ...

  5. Python制作回合制手游外挂简单教程(中)

    接着上篇的博文,今天我们讲如何实现自动组队刷道 引入: 自动组队刷道的流程是先点击刷道按钮.再点击前往按钮.再点击便捷组队······ 这些操作上篇博文已经告诉我们怎么做了,利用picpick丈量坐标 ...

  6. JS常用时间处理方法

    这里会扩展一些JS常用时间处理方法,内置时间对象的方法不再赘述 -- 传送门:http://www.w3school.com.cn/js/jsref_obj_date.asp 时间格式化 -- 转换为 ...

  7. [转]Composite Keys With WebApi OData

    本文转自:http://chris.eldredge.io/blog/2014/04/24/Composite-Keys/ In our basic configuration we told the ...

  8. C# 视频转换类

    using System.Web; using System.Configuration; namespace DotNet.Utilities { public class VideoConvert ...

  9. Spring定时(任务)刷新-quartz

    Quartz是一个完全由java编写的开源作业调度框架.他可以与J2EE.J2SE集成,用与处理定时任务.定时刷新的需求.此处使用为与Spring项目集成. 在SpringMVC项目中使用quartz ...

  10. T4模板的一些配置(从EF数据更新)

    <#@ template debug="false" hostspecific="false" language="C#" #> ...