歡迎您光臨本站 註冊首頁

ssh信任與scp自動傳輸

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

由於公司有了備份的需要,需要使用scp來自動的傳輸,所以我根據網上的很多資料研究了很久,最後寫了一份滿足我公司需要的scp自動傳輸文檔,希望對大家有幫助.

環境:

發行版本redhat5.4 X64位

為了大家看著方便,我把主機名給寫出來 service:172.16.6.4 (nagios) client:172.16.6.2 (savecenter) 1、服務端生產ssh密鑰
  1. [root@nagios .ssh]# ssh-keygen (所有選項一直回車即可)
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. 9a:b4:90:fd:86:7c:c9:88:b9:f0:f5:97:e6:98:2d:de root@nagios

可以查看是否有id_rsa(私鑰)與id_rsa.pub(公鑰)文件

  1. [root@nagios tmp]# cd /root/.ssh
  2. [root@nagios .ssh]# ll
  3. total 12
  4. -rw------- 1 root root 1675 Jan 31 13:21 id_rsa
  5. -rw-r--r-- 1 root root 393 Jan 31 13:21 id_rsa.pub
  6. -rw-r--r-- 1 root root 1399 Jan 27 14:01 known_hosts
2、從服務端複製密鑰到客戶端

  1. [root@nagios .ssh]# ssh-copy-id -i /root/.ssh/id_rsa 172.16.6.2
  2. 0
  3. root@172.16.6.2's password: (輸入客戶端的root密碼)
  4. Now try logging into the machine, with "ssh '172.16.6.2'", and check in:
  5. .ssh/authorized_keys (可以在客戶端的/root/.ssh/里查看是否有authorized_keys 此文件,如果有就正常成功)
  6. to make sure we haven't added extra keys that you weren't expecting.

3、自動傳輸腳本(可以自己隨便命名,我使用的是scp.sh)

以下是腳本:
  1. #! /bin/sh
  2. while getopts f: OPT; do
  3. case $OPT in
  4. f| f)
  5. files="$OPTARG $files"
  6. ;;
  7. *)
  8. echo "usage: `basename $0` [-f hostfile] <from> <to>"
  9. exit 2
  10. esac
  11. done
  12. shift `expr $OPTIND - 1`
  13. if [ "" = "$files" ] ;then
  14. echo "usage: `basename $0` [-f hostfile] <from> <

    to>"
  15. exit
  16. fi
  17. for file in $files
  18. do
  19. if [ ! -f "$file" ] ;then
  20. echo "no hostlist file:$file"
  21. exit
  22. fi
  23. hosts="$hosts `cat $file`"
  24. done
  25. for host in $hosts; do
  26. echo "do $host"
  27. scp -r $1 root@$host:$2
  28. done

其中第32行(scp -r $1 root@$host:$2 )如果不加參數r的話,傳輸文件夾的時候就會出現not a regular file問題,請記住,並給此腳本764許可權(具體許可權看你的需要)

  1. [root@nagios .ssh]# chmod 764 scp.sh
4、建立主機列表(client的列表,如果有多個客戶端的話,每行是一個)
  1. vim /root/.ssh/hostlist
  2. 172.16.6.2

5、在服務端執行scp.sh腳本,把文件傳輸到客戶端

可以使用 scp.sh -f (主機列表文件) 服務端想要傳輸的文件 客戶端收到的路徑
  1. scp.sh -f (主機列表文件) 服務端想要傳輸的文件 客戶端收到的路徑
  2. [root@nagios .ssh]# /root/.ssh/scp.sh -f /root/.ssh/hostlist /tmp/test_scp_y_or_n /tmp/
  3. do 172.16.6.2

  4. 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文件即可
  1. [root@savecenter test]# cd /tmp/
  2. [root@savecenter tmp]# ll
  3. total 20
  4. drwxr-xr-x 2 lbs lbs 4096 Jan 31 13:48 hsperfdata_lbs
  5. drwx------ 2 root root 4096 Jan 18 12:42 keyring-6jKU3T
  6. srwxr-xr-x 1 root root 0 Jan 18 12:42 mapping-root
  7. drwx------ 2 root root 4096 Jan 18 12:42 orbit-root
  8. drwx------ 2 root root 4096 Jan 18 12:42 ssh-gKLdFn4423
  9. drwxr-xr-x 2 root root 4096 Jan 31 13:27 test
  10. -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次圍觀

http://coctec.com/docs/linux/show-post-46907.html