隨筆 - 175  文章 - 202  trackbacks - 0
          <2012年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          第一個Blog,記錄哈哈的生活

          常用鏈接

          留言簿(16)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Java links

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          ssh 免密碼登錄,需要使用公私鑰來認證@import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

          首先通過 ssh-keygen 生成一對公私鑰,默認在 ~/.ssh/id_rsa.pub 和 ~/.ssh/id_rsa,前面的是公鑰。
          認證就是要把這個文件加到遠程用戶目錄 ~/.ssh/authorized_keys 文件中,而且這個文件的權限不能被其它人訪問。

          下面的腳本能夠自動把這個文件放到遠程,方法是 ./addpk ip username password

          #!//usr/bin/expect
          set host [lrange $argv 0 0]
          set user [lrange $argv 1 1]
          set passwd [lrange $argv 2 2]
          proc ssh {cmd} {
              global user host passwd
              spawn ssh $user@$host "$cmd"
              expect {
                  "*conne*" {
                      send "yes\n"
                      expect "*password*"
                      send "$passwd\n"
                      expect eof
                  }
                  "*password*" {
                      send "$passwd\n"
                      expect eof
              }
              }
          }
          proc scp {src dest} {
              global user host passwd
              spawn scp $src $user@$host:$dest
              expect {
                  "*conne*" {
                      send "yes\n"
                      expect "*password*"
                      send "$passwd\n"
                      expect eof
                  }
                  "*password*" {
                      send "$passwd\n"
                      expect eof
                  }
              }
          }
          ssh "mkdir -p ~/.ssh"
          scp "/home/user1/.ssh/id_rsa.pub" "~/.ssh/id_rsa.pub"
          ssh "cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys"
          ssh "uniq ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp"
          ssh "mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys"
          ssh "chmod 700 ~/.ssh"
          ssh "chmod 600 ~/.ssh/*"

          posted on 2012-05-08 09:38 哈哈的日子 閱讀(738) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 石门县| 长汀县| 永善县| 澄城县| 阳山县| 平凉市| 清远市| 巴彦淖尔市| 石景山区| 汉阴县| 聊城市| 新和县| 辰溪县| 葫芦岛市| 商城县| 荣昌县| 湟中县| 鄂尔多斯市| 利津县| 邓州市| 临安市| 江达县| 张家界市| 贺州市| 苏州市| 垦利县| 新乐市| 渑池县| 沧州市| 濮阳县| 朝阳县| 渭源县| 大渡口区| 平定县| 文昌市| 沙田区| 长春市| 随州市| 乐至县| 安平县| 仁化县|