由於公司有了備份的需要,需要使用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(公鑰)文件
2、從服務端複製密鑰到客戶端
- [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
- [root@nagios .ssh]# ssh-copy-id -i /root/.ssh/id_rsa 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許可權(具體許可權看你的需要)
4、建立主機列表(client的列表,如果有多個客戶端的話,每行是一個)
- [root@nagios .ssh]# chmod 764 scp.sh
- 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,為了大家方便所以上傳.
本文出自 「吟—技術交流」 博客,請務必保留此出處http://dl528888.blog.51cto.com/2382721/769519
[火星人 ] ssh信任與scp自動傳輸已經有717次圍觀