iTerm2快速SSH連接並保存密碼


 

背景

Mac自帶terminal,以及比較好用的iTerm2命令行工具,都缺乏一個功能,就是遠程SSH連接,無法保存密碼。
一種方法是將本機的ssh_key放到遠程服務器中實現無密碼登錄。這種方法在很多情況下無法實現,因為遠程服務器大多是客戶的。
本文介紹一個簡單、輕量級的命令行工具——sshpass,通過它我們能夠向命令提示符本身提供密碼(非交互式密碼驗證),這樣就可以實現自動連接遠程服務器,而且能自動執行遠程命令。

安裝sshpass

  1. 下載sshpass:https://sourceforge.net/projects/sshpass/files/
  2. 進入 sshpass目錄
  3. 運行【./configure】
  4. 運行【sudo make install】
  5. 運行【sshpass 】 來測試是否安裝成功

sshpass使用

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename   Take password to use from file
-d number     Use number as file descriptor for getting password
-p password   Provide password as argument (security unwise)
-e            Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-h            Show help (this screen)
-V            Print version information
At most one of -f, -d, -p or -e should be used

使用用戶名和密碼登錄到遠程Linux ssh服務器(10.42.0.1),並檢查文件系統磁盤使用情況,如圖所示。

$ sshpass -p 'my_pass_here' ssh aaronkilik@10.42.0.1 'df -h' 

也可以使用sshpass 通過scp傳輸文件或者rsync備份/同步文件,如下所示:

------- Transfer Files Using SCP ------- 
$ scp -r /var/www/html/example.com --rsh="sshpass -p 'my_pass_here' ssh -l aaronkilik" 10.42.0.1:/var/www/html
------- Backup or Sync Files Using Rsync -------
$ rsync --rsh="sshpass -p 'my_pass_here' ssh -l aaronkilik" 10.42.0.1:/data/backup/ /backup/

iTerm2集成sshpass實現快速SSH連接

  • 打開iTerm2的Profiles菜單,進入Profiles設置。
  • 點擊Edit Profiles。
  • 增加SSH連接。
    • Name:名稱
    • Tags:分組或者標簽名稱
    • Title:設置窗口名稱
    • Command:/usr/local/bin/sshpass -p 'xxxx' ssh root@192.168.129.116
  • 快速連接

參考資料

  1. sshpass:一個很棒的免交互SSH登錄工具,但不要用在生產服務器上
  2. iTerm2 保存ssh用戶名密碼






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM