Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63026
  • 博文数量: 7
  • 博客积分: 901
  • 博客等级: 准尉
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-07 14:44
文章分类

全部博文(7)

文章存档

2011年(6)

2010年(1)

分类: LINUX

2011-12-05 12:03:59

在工作中经常遇到给两台主机建立ssh信任,手动建立太费事了,索性胡乱写了个脚本ssh_trust.sh来自动建立信任:

  1. #!/bin/bash
  2. src_host=$1
  3. src_username=$2
  4. src_passwd=$3

  5. dst_host=$4
  6. dst_username=$5
  7. dst_passwd=$6

  8. #在远程主机1上生成公私钥对
  9. Keygen()
  10. {
  11. expect << EOF

  12. spawn ssh $src_username@$src_host ssh-keygen -t rsa
  13. while 1 {

  14. expect {
  15. "password:" {
  16. send "$src_passwd\n"
  17. }
  18. "yes/no*" {
  19. send "yes\n"
  20. }
  21. "Enter file in which to save the key*" {
  22. send "\n"
  23. }
  24. "Enter passphrase*" {
  25. send "\n"
  26. }
  27. "Enter same passphrase again:" {
  28. send "\n"
  29. }

  30. "Overwrite (y/n)" {
  31. send "n\n"
  32. }
  33. eof {
  34. exit
  35. }

  36. }
  37. }
  38. EOF
  39. }

  40. #从远程主机1获取公钥保存到本地
  41. Get_pub()
  42. {
  43. expect << EOF

  44. spawn scp $src_username@$src_host:~/.ssh/id_rsa.pub /tmp
  45. expect {
  46. "password:" {
  47. send "$src_passwd\n";exp_continue
  48. }
  49. "yes/no*" {
  50. send "yes\n";exp_continue
  51. }
  52. eof {
  53. exit
  54. }
  55. }
  56. EOF
  57. }
  58. #将公钥的内容附加到远程主机2的authorized_keys
  59. Put_pub()
  60. {
  61. src_pub="$(cat /tmp/id_rsa.pub)"
  62. expect << EOF
  63. spawn ssh $dst_username@$dst_host "chmod 700 ~/.ssh;echo $src_pub >> ~/.ssh/authorized_keys;chmod 600 ~/.ssh/authorized_ke
  64. ys"
  65. expect {
  66. "password:" {
  67. send "$dst_passwd\n";exp_continue
  68. }
  69. "yes/no*" {
  70. send "yes\n";exp_continue
  71. }
  72. eof {
  73. exit
  74. }
  75. }
  76. EOF
  77. }
  78. Keygen
  79. Get_pub
  80. Put_pub
脚本主要由3个expect组成,比较简单,用法
  1. ./ssh_trust.sh host1 user1 passwd1 host2 user2 passwd2

即建立从user1@host1到user2@host2的ssh信任。
阅读(1820) | 评论(0) | 转发(0) |
0

上一篇:转载【FTP】vsftpd日志(xferlog格式)的含义

下一篇:没有了

给主人留下些什么吧!~~