隨筆 - 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 哈哈的日子 閱讀(739) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 资阳市| 马山县| 沙坪坝区| 益阳市| 东方市| 贺兰县| 宁都县| 镇康县| 维西| 穆棱市| 内黄县| 江安县| 灵武市| 乌鲁木齐市| 巴塘县| 富宁县| 荣昌县| 含山县| 伊春市| 博乐市| 陆川县| 通山县| 阿鲁科尔沁旗| 封丘县| 阿克苏市| 类乌齐县| 荥经县| 鄂托克旗| 苗栗市| 湘潭市| 怀安县| 贵州省| 塔河县| 远安县| 普兰店市| 嘉禾县| 文昌市| 乌鲁木齐县| 常熟市| 万载县| 米林县|