案例

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

[root@xw4 tmp]# cat test-ssh.sh
#!/bin/bash
passwd='PASSWORD'
/usr/local/bin/expect <<-EOF
set time 30
spawn ssh -p22 root@192.168.1.201
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$passwd\r" }
}
expect "*#"
send "useradd wangshibo\r"
expect "*#"
send "mkdir /opt/test\r"
expect "*#"
send "exit\r"
interact
expect eof
EOF [root@xw4 tmp]# sh test.sh
spawn ssh -p22 root@192.168.1.201
root@192.168.1.201's password:
Last login: Fri Sep 23 16:21:20 2016 from 192.168.1.23
[root@vm-002 ~]# useradd wangshibo
[root@vm-002 ~]# mkdir /opt/test
[root@vm-002 ~]# [root@xw4 tmp]#

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

(1)
如上expect安装后的路径是:
[root@xw4 ~]# which expect
/usr/local/bin/expect (2)
做个expect执行文件的软件
[root@xw4 ~]# ln -s /usr/local/bin/expect /usr/bin/expect
[root@xw4 ~]# ll /usr/bin/expect (3)
编写expect脚本:
-----------------------------------------------------------------------------------
1)
[root@xw4 ~]# cat sshkey.exp
#!/usr/bin/expect #sshkey.exp if {$argc<3} {
puts stderr "Usage: $argv0 host user passwd "
exit 1
} set host [ lindex $argv 0 ]
set user [ lindex $argv 1 ]
set pwd [ lindex $argv 2 ] set timeout 30 #spawn ssh ${user}@${host} "rm -rf ~/.ssh/id_rsa*"
#
#expect {
# "*yes/no" { send "yes\r"; exp_continue }
# "*password:" { send "$pwd\r"; exp_continue }
#} spawn ssh ${user}@${host} "ssh-keygen -t rsa" expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r"; exp_continue }
"Enter file in which to save the key*" { send "\n\r"; exp_continue }
"Overwrite*" { send "y\n"; exp_continue }
"Enter passphrase (empty for no passphrase):" { send "\n\r"; exp_continue }
"Enter same passphrase again:" { send "\n\r" }
} spawn ssh ${user}@${host} "cat ~/.ssh/id_rsa.pub" expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r" }
} expect eof ----------------------------------------------------------------------------------------------------
2)
[root@xw4 ~]# cat noscp.exp
#!/usr/bin/expect #noscp.exp if {$argc<4} {
puts stderr "Usage: $argv0 localfile remotefile user passwd "
exit 1
} set localfile [ lindex $argv 0 ]
set remotefile [ lindex $argv 1 ]
set user [ lindex $argv 2 ]
set pwd [ lindex $argv 3 ] set timeout 30 spawn scp ${localfile} ${user}@${remotefile} expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r" }
} expect eof ------------------------------------------------------------------------ [root@xw4 ~]# chmod 755 sshkey.exp
[root@xw4 ~]# chmod 755 noscp.exp (4)
脚本说明
./sshkey.exp 主机名 用户名 密码 (在远程主机生成id_rsa)
./noscp.exp 本地文件 远程路径 远程用户密码 (无密码拷贝文件) (5)验证:
[root@xw4 ~]# ./sshkey.exp 192.168.1.201 root PASSWORD |grep ssh-rsa >> ~/.ssh/authorized_keys
[root@xw4 ~]# ./noscp.exp ~/.ssh/authorized_keys 192.168.1.201:~/.ssh root PASSWORD
spawn scp /root/.ssh/authorized_keys root@192.168.1.201:~/.ssh
root@192.168.1.201's password:
authorized_keys 这样,就能无密码登陆了!
[root@xw4 ~]# ssh 192.168.1.201
Last login: Fri Sep 23 18:33:21 2016 from 192.168.1.7
[root@vm-002 ~]# --------------------------------------------------------------------------
如果是多台机器的话,可以结合shell脚本进行批量执行 [root@xw4 ~]# cat /root/ip.list
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
......
...... [root@xw4 ~]# cat sshkey.sh
#!/bin/bash
user='root'
password='PASSWORD'
for ip in `cat /root/ip.list`
do
/root/sshkey.exp $ip $user $password |grep ssh-rsa >> ~/.ssh/authorized_keys
/root/noscp.exp ~/.ssh/authorized_keys $user@$ip:~/.ssh root PASSWORD
done

其他

dispatch_copy.exp

#! /usr/local/bin/expect
if { $argc != 3 } {
send_user "usage :expect dispatch_sshkey.expect file host dest \n"
} #define var
set timeout 3
set file [lindex $argv 0]
set host [lindex $argv 1]
set dest [lindex $argv 2]
set password "hhh"
#spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
#spawn scp -P11544 $file root@$host:$dir
#spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
#spawn ssh-copy-id -i .ssh/id_dsa.pub omd@192.168.25.137
spawn scp $file oldgirl@$host:$dest
expect of {
#timeout 1
"yes/no" {send "yes\r"}
"*password" {send "$password\r"}
timeout {puts "Expect was timeout please contact ftl at XXXXX"; return}
} exit -onexit {
send_user "good bye!! see you \n"
}

dispatch-sshkey.exp

#! /usr/local/bin/expect
if { $argc !=2 } {
send_user "usage :expect dispatch_sshkey.expect file host \n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "hhh"
#spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
#spawn scp -P11544 $file root@$host:$dir
#spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
#spawn ssh-copy-id -i .ssh/id_dsa.pub omd@192.168.25.137
spawn ssh-copy-id -i $file oldgirl@$host
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
 

dispatch-sshkey2.exp[更完善]

#! /usr/local/bin/expect
if { $argc !=2 } {
send_user "usage :expect dispatch_sshkey.expect file host \n"
exit
}
#define var
set timeout 1
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "hhh"
#spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
#spawn scp -P11544 $file root@$host:$dir
#spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
#spawn ssh-copy-id -i .ssh/id_dsa.pub omd@192.168.25.137
spawn ssh-copy-id -i $file oldgirl@$host
expect {
#timeout 1
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
timeout {puts "Expect was timeout "; return}
}
expect eof
exit -onexit {
exec rm $tmpfile
send_user "good bye\n"
}

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. HttpClient使用详细教程

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...

  2. Linux Ubuntu系统下Java开发环境搭建

    操作系统:Linux x64 / Ubuntu 14.04 Java JDK版本:jdk-8u65-linux-x64.tar.gz 声明:转载请注明出处及本文链接 1. 前往ORACLE官网下载最新 ...

  3. latex排版(CTeX winEdit输出“系统找不到指定的文件”的终极解决办法)

    WinEdit的菜单栏中:Options->Execution Modes 弹出的界面见下面的图: 看上图中“1”所示位置,后面3项出现了“?”号(出现“系统找不到指定的文件”的错误时,所有项后 ...

  4. ASP.NET Core 中的配置

    目录 以键-值对的形式读取配置 多环境配置 读取结构化的配置数据 参考 .NET Core 定义配置的方式不同于之前 NET 版本,之前是依赖于 System.Configuration 的 app. ...

  5. [PY3]——合并多个字典或映射(collections模块中的ChainMap 类)

    问题 现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作, 比如查找值或者检查某些键是否存在. 解决方案 使用 collections 模块中的 ChainMap 类 Cha ...

  6. HDFS常用shell命令

    注,其实常用命令不用网上搜,和linux下的命令很类似,触类旁通,直接在linux 上 hadoop fs 看一下就行了,不需要刻意去记我把 linux 上的 help 列举下,方便直接看吧,hdfs ...

  7. pureMVC与strangeIoc框架对比

    前言 最近有机会了解到了StrangeIoc框架,就拿来跟自己比较熟悉的pureMVC进行一下简要的对比.这两套开源框架都是基于MVC模式的扩展,pureMVC是一个跨平台跨语言的MVC轻量级应用框架 ...

  8. 京东-Java开发工程师-一面

    时间:2017-4-7 16:47 时长:32分19秒 类型:笔试前电话面试 之前打过一个电话过来说了一声,下午就直接打过来面试了,没有自我介绍貌似 1. 你做的这些东西是什么样的? 2. 选一个你觉 ...

  9. 七、并发容器ConcurrentHashMap

    一.简介 我们知道,HashMap是线程不安全的.而HashTable是线程安全的,但是JDK已经不建议使用HashTable,它已经被作为废除的实现. 在JDK并发包里面,ConcurrentHas ...

  10. [LeetCode]Delete and Earn题解(动态规划)

    Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...