推送文件(expect交互式方式与sshpass非交互式方式)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了推送文件(expect交互式方式与sshpass非交互式方式)相关的知识,希望对你有一定的参考价值。

前提条件

以下两种推送的文件是SSH的公钥,推送方式依赖于SSH,基于SSH的相关方面操作和机器相关配置请参考我的上一篇博文“ssh服务批量管理例子


1、expect交互式方式

1、1上传文件及文件内容

#以10机器为例。

#执行rz –y分别上传“dis-sshkey”、“expect-copy-sshkey.exp”和“iplist”,三者文件内容如下:

[[email protected] ~]$ ls -l dis-sshkey  expect-copy-sshkey.exp  iplist

-rw-r--r-- 1 oldgirl oldgirl 287 Jun  8 22:55 dis-sshkey

-rw-r--r-- 1 oldgirl oldgirl 579 Jun  8 22:55 expect-copy-sshkey.exp

-rw-r--r-- 1 oldgirl oldgirl  23 Jun 8 22:55 iplist


#dis-sshkey的内容如下:

[[email protected] ~]$ cat dis-sshkey

#!/bin/sh
. /etc/init.d/functions
[ ! -f iplist ] && echo "hostlists.txt isnot exist." && exit 1
 
for host in `cat iplist`
do
 expectexpect-copy-sshkey.exp $host &>/dev/null
 if [ $? -eq 0];then
    action "$ipdis data" /bin/true
 else
    action"$ip dis data" /bin/false
 fi
done


 

#expect-copy-sshkey.exp的内容如下:

[[email protected] ~]$ cat expect-copy-sshkey.exp

#!/usr/bin/expect
if { $argc != 1 } {  
 send_user"usage: expect expect-copy-sshkey.exp host \n"   
 exit   
} 
 
#define var
set host [lindex $argv 0]
set password "123456"
 
spawn ssh-copy-id -i /home/oldgirl/.ssh/id_dsa.pub"[email protected]$host"
#spawn ssh -p 50718 [email protected]$host /sbin/ifconfig 
set timeout 60
expect {
        -timeout 20
       "yes/no"    {send"yes\r";exp_continue}
       "*password" {send "$password\r"}
       timeout  {puts "expectconnect timeout,pls contact oldboy."; return}
}
expect eof
 
exit -onexit {
  send_user"Oldboy say good bye to you!\n"
}


#iplist的内容如下:

[[email protected] ~]$ cat iplist

172.16.1.7

172.16.1.9


1、2安装expect

[[email protected] ~]# rpm -qa expect

[[email protected] ~]# yum install expect -y

Loaded plugins: fastestmirror, security
Setting up Install Process
Determining fastest mirrors
 * base:centos.ustc.edu.cn
 * extras:centos.ustc.edu.cn
 * updates:centos.ustc.edu.cn
base                                                                                                        | 3.7 kB     00:00     
extras                                                                                                      | 3.4 kB     00:00     
updates                                                                                                     | 3.4 kB     00:00     
updates/primary_db                                                                                          | 729 kB     00:00     
Resolving Dependencies
--> Running transaction check
---> Package expect.x86_64 0:5.44.1.15-5.el6_4 will beinstalled
--> Finished Dependency Resolution
 
Dependencies Resolved
 
====================================================================================================================================
 Package                      Arch                         Version                                Repository                  Size
====================================================================================================================================
Installing:
 expect                       x86_64                       5.44.1.15-5.el6_4                       base                       256 k
 
Transaction Summary
====================================================================================================================================
Install       1Package(s)
 
Total download size: 256 k
Installed size: 553 k
Downloading Packages:
expect-5.44.1.15-5.el6_4.x86_64.rpm                                                                         | 256 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing :expect-5.44.1.15-5.el6_4.x86_64                                                                                 1/1 
  Verifying  : expect-5.44.1.15-5.el6_4.x86_64                                                                                  1/1
 
Installed:
  expect.x86_640:5.44.1.15-5.el6_4                                                                                                
 
Complete!


[[email protected] ~]# rpm -qa expect

expect-5.44.1.15-5.el6_4.x86_64

[[email protected] ~]# which expect

/usr/bin/expect


1、3同步公钥操作

#先把79机器中已存在的公钥文件删除,不然再同步公钥的时候会失败。

[[email protected] ~]$ rm -rf ~/.ssh/

[[email protected] ~]$ rm -rf ~/.ssh/

 

#在10机器执行

[[email protected] ~]$ /bin/sh ~/dis-sshkey

 dis data                                                 [  OK  ]

 dis data                                                 [  OK  ]

 

#在79机器查看结果

[[email protected] ~]$ ls -l ~/.ssh/authorized_keys

-rw------- 1 oldgirl oldgirl 604 Jun  8 23:26 /home/oldgirl/.ssh/authorized_keys

[[email protected] ~]$ ls -l ~/.ssh/authorized_keys

-rw------- 1 oldgirl oldgirl 604 Jun  8 23:26 /home/oldgirl/.ssh/authorized_keys


2、sshpass非交互式

2、1安装sshpass

[[email protected] oldgirl]# cd /etc/yum.repos.d/

[[email protected] yum.repos.d]# ll

total 24

-rw-r--r--. 1 root root 1991 Aug  4  2015CentOS-Base.repo

-rw-r--r--. 1 root root 647 Aug  4  2015 CentOS-Debuginfo.repo

-rw-r--r--. 1 root root 289 Aug  4  2015 CentOS-fasttrack.repo

-rw-r--r--. 1 root root 630 Aug  4  2015 CentOS-Media.repo

-rw-r--r--. 1 root root 6259 Aug  4  2015CentOS-Vault.repo

[[email protected] yum.repos.d]# wgethttp://download.opensuse.org/repositories/home:/Strahlex/CentOS_CentOS-6/home:Strahlex.repo

#上述命令是在一行的。

[[email protected] yum.repos.d]# yum install sshpass

 

2、2操作

[[email protected] ~]$ sshpass -p 123456 ssh 172.16.1.31

#第一次操作可能没反应,可用下述参数解决

[[email protected] ~]# sshpass -p 123456 ssh -p 22 172.16.1.31 -o StrictHostKeyChecking=no

Warning: Permanently added ‘172.16.1.31‘ (RSA) to thelist of known hosts.

Last login: Fri Jun 10 15:32:52 2016 from 10.0.0.1

[[email protected] ~]# logout

[[email protected] ~]# sshpass -p 123456 ssh 172.16.1.31 -o StrictHostKeyChecking=no

Last login: Fri Jun 10 15:57:47 2016 from 172.16.1.41

[[email protected] ~]# logout

2、2、1#直接带密码连接服务器

[[email protected] ~]# sshpass -p 123456 ssh 172.16.1.31

Last login: Fri Jun 10 15:57:35 2016 from 172.16.1.41

[[email protected] ~]# logout

2、2、2#以文件形式保存密码连接服务器

[[email protected] ~]# echo "123456">ssh.passwd

[[email protected] ~]# sshpass -f ssh.passwd ssh 172.16.1.31

Last login: Fri Jun 10 16:00:06 2016 from 172.16.1.41

[[email protected] ~]# logout

Connection to 172.16.1.31 closed.

2、2、3#将密码写入变量

[[email protected] ~]# export SSHPASS="123456"

[[email protected] ~]# sshpass -e ssh 172.16.1.31

Last login: Fri Jun 10 16:05:14 2016 from 172.16.1.41

[[email protected] ~]# logout

Connection to 172.16.1.31 closed.

2、2、4#同理远程拉取、推送文件、执行命令等操作scp,rsync均可。

[[email protected] ~]# sshpass -f ssh.passwd scp /etc/hosts172.16.1.31:/root/

[[email protected] ~]# pwd

/root

[[email protected] ~]# ls -l hosts

-rw-r--r-- 1 root root 158 Jun 10 16:13 hosts

[[email protected] ~]# sshpass -f ssh.passwd ssh 172.16.1.31 ls /root/

anaconda-ks.cfg

hosts

install.log

install.log.syslog




以上是关于推送文件(expect交互式方式与sshpass非交互式方式)的主要内容,如果未能解决你的问题,请参考以下文章

sshpass+expect解决交互式问题

利用expect和sshpass完美非交互性执行远端命令

expect非交互式功能实战

Linux 下方便的ssh非交互工具sshpass的安装与使用

sshpass之非交互SSH密码验证

sshpass: 用于非交互的ssh 密码验证