Linxu下 expect的安装与使用2
Posted ftl1012
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linxu下 expect的安装与使用2相关的知识,希望对你有一定的参考价值。
案例
例1:
从本机自动登录到远程机器192.168.1.200(端口是22,密码是:PASSWORD)
登录到远程机器后做以下几个操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit自动退出
[[email protected] tmp]# cat test-ssh.sh #!/bin/bash passwd=‘PASSWORD‘ /usr/local/bin/expect <<-EOF set time 30 spawn ssh -p22 [email protected] expect { "*yes/no" { send "yes "; exp_continue } "*password:" { send "$passwd " } } expect "*#" send "useradd wangshibo " expect "*#" send "mkdir /opt/test " expect "*#" send "exit " interact expect eof EOF [[email protected] tmp]# sh test.sh spawn ssh -p22 [email protected] [email protected]‘s password: Last login: Fri Sep 23 16:21:20 2016 from 192.168.1.23 [[email protected] ~]# useradd wangshibo [[email protected] ~]# mkdir /opt/test [[email protected] ~]# [[email protected] tmp]#
例2:我们在部署无密码访问时,手工建立ssh互信需要好几个步骤,并且中途人工交互(输入密码等),如果机器数目多,则很繁琐!
下面方法用于自动化生成authorized_keys,免去了手工数据.
方法: 利用expect编写sshkey.exp在远程主机上生成id_rsa,并重定向到本地.在利用noscp.exp.把文件复制到远程主机
为了节省自己的时间,可以写个expect自动化脚本,分享如下:
(1) 如上expect安装后的路径是: [[email protected] ~]# which expect /usr/local/bin/expect (2) 做个expect执行文件的软件 [[email protected] ~]# ln -s /usr/local/bin/expect /usr/bin/expect [[email protected] ~]# ll /usr/bin/expect (3) 编写expect脚本: ----------------------------------------------------------------------------------- 1) [[email protected] ~]# 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 "; exp_continue } # "*password:" { send "$pwd "; exp_continue } #} spawn ssh ${user}@${host} "ssh-keygen -t rsa" expect { "*yes/no" { send "yes "; exp_continue } "*password:" { send "$pwd "; exp_continue } "Enter file in which to save the key*" { send " "; exp_continue } "Overwrite*" { send "y "; exp_continue } "Enter passphrase (empty for no passphrase):" { send " "; exp_continue } "Enter same passphrase again:" { send " " } } spawn ssh ${user}@${host} "cat ~/.ssh/id_rsa.pub" expect { "*yes/no" { send "yes "; exp_continue } "*password:" { send "$pwd " } } expect eof ---------------------------------------------------------------------------------------------------- 2) [[email protected] ~]# 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 "; exp_continue } "*password:" { send "$pwd " } } expect eof ------------------------------------------------------------------------ [[email protected] ~]# chmod 755 sshkey.exp [[email protected] ~]# chmod 755 noscp.exp (4) 脚本说明 ./sshkey.exp 主机名 用户名 密码 (在远程主机生成id_rsa) ./noscp.exp 本地文件 远程路径 远程用户密码 (无密码拷贝文件) (5)验证: [[email protected] ~]# ./sshkey.exp 192.168.1.201 root PASSWORD |grep ssh-rsa >> ~/.ssh/authorized_keys [[email protected] ~]# ./noscp.exp ~/.ssh/authorized_keys 192.168.1.201:~/.ssh root PASSWORD spawn scp /root/.ssh/authorized_keys [email protected]:~/.ssh [email protected]‘s password: authorized_keys 这样,就能无密码登陆了! [[email protected] ~]# ssh 192.168.1.201 Last login: Fri Sep 23 18:33:21 2016 from 192.168.1.7 [[email protected] ~]# -------------------------------------------------------------------------- 如果是多台机器的话,可以结合shell脚本进行批量执行 [[email protected] ~]# cat /root/ip.list 192.168.1.100 192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104 ...... ...... [[email protected] ~]# 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 [email protected]$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 " } #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/omc/2017-08-23 [email protected]:/home/omc #spawn scp -P11544 $file [email protected]$host:$dir #spawn ssh-copy-id -i $file "-p 11544 [email protected]$host:$dir" #spawn ssh-copy-id -i .ssh/id_dsa.pub [email protected] spawn scp $file [email protected]$host:$dest expect of { #timeout 1 "yes/no" {send "yes "} "*password" {send "$password "} timeout {puts "Expect was timeout please contact ftl at XXXXX"; return} } exit -onexit { send_user "good bye!! see you " }
dispatch-sshkey.exp
#! /usr/local/bin/expect if { $argc !=2 } { send_user "usage :expect dispatch_sshkey.expect file host " exit } #define var set file [lindex $argv 0] set host [lindex $argv 1] set password "hhh" #spawn scp /home/omc/2017-08-23 [email protected]:/home/omc #spawn scp -P11544 $file [email protected]$host:$dir #spawn ssh-copy-id -i $file "-p 11544 [email protected]$host:$dir" #spawn ssh-copy-id -i .ssh/id_dsa.pub [email protected] spawn ssh-copy-id -i $file [email protected]$host expect { "yes/no" {send "yes ";exp_continue} "*password" {send "$password "} } expect eof
dispatch-sshkey2.exp[更完善]
#! /usr/local/bin/expect if { $argc !=2 } { send_user "usage :expect dispatch_sshkey.expect file host " exit } #define var set timeout 1 set file [lindex $argv 0] set host [lindex $argv 1] set password "hhh" #spawn scp /home/omc/2017-08-23 [email protected]:/home/omc #spawn scp -P11544 $file [email protected]$host:$dir #spawn ssh-copy-id -i $file "-p 11544 [email protected]$host:$dir" #spawn ssh-copy-id -i .ssh/id_dsa.pub [email protected] spawn ssh-copy-id -i $file [email protected]$host expect { #timeout 1 "yes/no" {send "yes ";exp_continue} "*password" {send "$password "} timeout {puts "Expect was timeout "; return} } expect eof exit -onexit { exec rm $tmpfile send_user "good bye " }
以上是关于Linxu下 expect的安装与使用2的主要内容,如果未能解决你的问题,请参考以下文章
报错:✘ http://eslint.org/docs/rules/indent Expected indentation of 0 s paces but found 2(代码片段
报错:✘ http://eslint.org/docs/rules/indent Expected indentation of 0 s paces but found 2(代码片段
在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途