由于公司有了备份的需要,需要使用scp来自动的传输,所以我根据网上的很多资料研究了很久,最后写了一份满足我公司需要的scp自动传输文档,希望对大家有帮助。
环境:
发行版本redhat5.4 X64位
为了大家看着方便,我把主机名给写出来
service:172.16.6.4
(nagios)
client:172.16.6.2
(savecenter)
1、服务端生产ssh密钥
- [root@nagios .ssh]# ssh-keygen (所有选项一直回车即可)
- Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.ssh/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /root/.ssh/id_rsa.
- Your public key has been saved in /root/.ssh/id_rsa.pub.
- The key fingerprint is:
- 9a:b4:90:fd:86:7c:c9:88:b9:f0:f5:97:e6:98:2d:de root@nagios
可以查看是否有id_rsa(私钥)与id_rsa.pub(公钥)文件
- [root@nagios tmp]# cd /root/.ssh
- [root@nagios .ssh]# ll
- total 12
- -rw------- 1 root root 1675 Jan 31 13:21 id_rsa
- -rw-r--r-- 1 root root 393 Jan 31 13:21 id_rsa.pub
- -rw-r--r-- 1 root root 1399 Jan 27 14:01 known_hosts
2、从服务端复制密钥到客户端
- [root@nagios .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.6.2
- 0
- root@172.16.6.2's password: (输入客户端的root密码)
- Now try logging into the machine, with "ssh '172.16.6.2'", and check in:
- .ssh/authorized_keys (可以在客户端的/root/.ssh/里查看是否有authorized_keys 此文件,如果有就正常成功)
- to make sure we haven't added extra keys that you weren't expecting.
3、自动传输脚本(可以自己随便命名,我使用的是scp.sh)
以下是脚本:
- #! /bin/sh
- while getopts f: OPT; do
- case $OPT in
- f|+f)
- files="$OPTARG $files"
- ;;
- *)
- echo "usage: `basename $0` [-f hostfile] <from> <to>"
- exit 2
- esac
- done
- shift `expr $OPTIND - 1`
- if [ "" = "$files" ] ;then
- echo "usage: `basename $0` [-f hostfile] <from> <to>"
- exit
- fi
- for file in $files
- do
- if [ ! -f "$file" ] ;then
- echo "no hostlist file:$file"
- exit
- fi
- hosts="$hosts `cat $file`"
- done
- for host in $hosts; do
- echo "do $host"
- scp -r $1 root@$host:$2
- done
其中第32行(scp -r $1 root@$host:$2 )如果不加参数r的话,传输文件夹的时候就会出现not a regular file问题,请记住,并给此脚本764权限(具体权限看你的需要)
- [root@nagios .ssh]# chmod 764 scp.sh
4、建立主机列表(client的列表,如果有多个客户端的话,每行是一个)
- vim /root/.ssh/hostlist
- 172.16.6.2
5、在服务端执行scp.sh脚本,把文件传输到客户端
可以使用
scp.sh -f (主机列表文件) 服务端想要传输的文件 客户端收到的路径
- scp.sh -f (主机列表文件) 服务端想要传输的文件 客户端收到的路径
- [root@nagios .ssh]# /root/.ssh/scp.sh -f /root/.ssh/hostlist /tmp/test_scp_y_or_n /tmp/
- do 172.16.6.2
- test_scp_y_or_n 100% 0 0.0KB/s 00:00
其中/root/.ssh/hostlist是主角列表文件
/tmp/test_scp_y_or_n是服务端想要传输的文件
/tmp/客户端收到的路径
然后登陆客户端查看/tmp/是否有test_scp_y_or_n文件即可
- [root@savecenter test]# cd /tmp/
- [root@savecenter tmp]# ll
- total 20
- drwxr-xr-x 2 lbs lbs 4096 Jan 31 13:48 hsperfdata_lbs
- drwx------ 2 root root 4096 Jan 18 12:42 keyring-6jKU3T
- srwxr-xr-x 1 root root 0 Jan 18 12:42 mapping-root
- drwx------ 2 root root 4096 Jan 18 12:42 orbit-root
- drwx------ 2 root root 4096 Jan 18 12:42 ssh-gKLdFn4423
- drwxr-xr-x 2 root root 4096 Jan 31 13:27 test
- -rw-r--r-- 1 root root 0 Jan 31 13:44 test_scp_y_or_n
可以看到在客户端里收到了test_scp_y_or_n文件,证明scp信任成功的建立,不同服务器直接的scp传输可以不需要输入密码,如果你想要自动的实现scp传输,那么你在crontab里设置自动传输的时间、服务器传输的文件、客户端接收文件的位置即可,如果想要实现1台服务端传到多台客户端,那么你在hostlist里添加多个客户端的ip即可。
BTW:附件是scp.sh脚本的,rar格式解压即为scp.sh,为了大家方便所以上传。
本文转自 dl528888 51CTO博客,原文链接:http://blog.51cto.com/dl528888/769519,如需转载请自行联系原作者