YAZONG 我的开源

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

  , ,
0 评论0 浏览

前提条件

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

1、expect交互式方式

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

#以10机器为例。

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

[oldgirl@backup ~]$ 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的内容如下:

[oldgirl@backup ~]$ 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的内容如下:

[oldgirl@backup ~]$ 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"oldgirl@$host"
#spawn ssh -p 50718 disdata@$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的内容如下:

[oldgirl@backup ~]$ cat iplist

172.16.1.7

172.16.1.9

1、2安装expect

[root@backup ~]# rpm -qa expect

[root@backup ~]# 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!

[root@backup ~]# rpm -qa expect

expect-5.44.1.15-5.el6_4.x86_64

[root@backup ~]# which expect

/usr/bin/expect

1、3同步公钥操作

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

[oldgirl@lnmp01 ~]$ rm -rf ~/.ssh/

[oldgirl@nfs-server ~]$ rm -rf ~/.ssh/

#在10机器执行

[oldgirl@backup ~]$ /bin/sh ~/dis-sshkey

dis data [ OK ]

dis data [ OK ]

#在7和9机器查看结果

[oldgirl@lnmp01 ~]$ ls -l ~/.ssh/authorized_keys

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

[oldgirl@nfs-server ~]$ ls -l ~/.ssh/authorized_keys

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

2、sshpass非交互式

**2、1安装sshpass

[root@backup oldgirl]# cd /etc/yum.repos.d/

[root@backup 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

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

#上述命令是在一行的。

[root@backup yum.repos.d]# yum install sshpass

2、2操作

[oldboy@backup ~]$ sshpass -p 123456 ssh 172.16.1.31

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

[root@backup ~]# 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

[root@nfs01 ~]# logout

[root@backup ~]# 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

[root@nfs01 ~]# logout

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

[root@backup ~]# sshpass -p 123456 ssh 172.16.1.31

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

[root@nfs01 ~]# logout

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

[root@backup ~]# echo "123456">ssh.passwd

[root@backup ~]# sshpass -f ssh.passwd ssh 172.16.1.31

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

[root@nfs01 ~]# logout

Connection to 172.16.1.31 closed.

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

[root@backup ~]# export SSHPASS="123456"

[root@backup ~]# sshpass -e ssh 172.16.1.31

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

[root@nfs01 ~]# logout

Connection to 172.16.1.31 closed.

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

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

[root@nfs01 ~]# pwd

/root

[root@nfs01 ~]# ls -l hosts

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

[root@backup ~]# sshpass -f ssh.passwd ssh 172.16.1.31 ls /root/

anaconda-ks.cfg

hosts

install.log

install.log.syslog

3、总结

sshpass的优点:简单易用,速度会比expect快很多。

sshpass的缺点:-p参数密码是明文显示,可以通过history查看。-f参数则需要对面文件进行权限限制,保证他人无法查看。-e参数别人也可以查到IP。


标题:推送文件(expect交互式方式与sshpass非交互式方式)
作者:yazong
地址:https://blog.llyweb.com/articles/2016/06/14/1578159816247.html