Mac安裝sshpass 替代spawn自動登入腳本(解決rz sz問題)
很早前就發現 如果通過 spawn 腳本去登入 伺服器,那麼 rz sz 指令是無法使用的,具體為什麼,網上說就是不支援!。。無奈 找到另外一個 sshpass 這個指令去解決
前言
附上我的 item2log.sh 檔案 在我 /usr/local/bin/下面
大緻意思就是 讀取輸入參數 然後通過spawn指令去 實作自動登入,很無奈 rz sz 無法使用
#! /usr/bin/expectset timeout 30spawn ssh -p [lindex $argv 0] [lindex $argv 1]@[lindex $argv 2]expect { "(yes/no)?" {send "yes\n";exp_continue} "password:" {send "[lindex $argv 3]\n"}}interact
上面的腳本 再配合 iterm2 使用 ,即可友善登入

好了 記錄一下上面的就行了,下面開始說 sshpass
Mac 安裝 sshpass(解決rz sz )
由于我要實作 在Mac 上使用 iterm2 登入各個伺服器,那麼我要在mac 上安裝 sshpass 來使用這個指令去登入
參考
https://www.sunjs.com/article/idetail/7891c859e64b4ec6a40a5cb672ba6d43.html
https://blog.csdn.net/liyuxing6639801/article/details/103172437
網上好多都失效了,2019-11-20以下方法親測可行。
brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb1
安裝成功後執行 sshpass出現以下效果,說明安裝成功。
➜ ~ sshpassUsage: 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 -P prompt Which string should sshpass search for to detect a password prompt -v Be verbose about what you're doing -h Show help (this screen) -V Print version informationAt most one of -f, -d, -p or -e should be used12345678910111213
安裝成功了,那就把密碼記下來吧 -p 是指密碼的意思
vim ~/.bashrc1alias jump="sshpass -p '666' ssh host"1
載入檔案 source ~/.bashrc, 具體是哪個檔案根據自身情況決定,我把.bashrc 加在了.zshrc裡,是以我執行的是
source ~/.zshrc
。
接下來就是驗證成果是時候了。
➜ ~ jumpLast login: Wed Nov 20 21:59:08 2019 from 172.16.113.151Welcome to Alibaba Cloud Elastic Compute Service !123
一開始我是按照 上面這個方式 放入 mac 的 ~/.bash_profile 通過 alias ,但是我發現 可能我後面有很多其他服務那麼 這樣 jump 不友善,我自己寫了個腳本
後續 隻要輸入
jump ali 就可以跳轉登入到 ali 輸入 jump --list 就列出所有的key 和描述
#!/bin/bashNULLSTR="--list"if [[ $1 = $NULLSTR ]] then echo "ali: (阿裡雲) 47.98.241.126 root password" echo "xuni: (虛拟機-centos7桌面版) 192.168.29.101 root root"elif [[ $1 = "ali" ]] then echo "ali: 47.98.241.126 root password" exec sshpass -p 'password' ssh [email protected] [[ $1 = "xuni" ]] then echo "xuni: 192.168.29.101 root root" exec sshpass -p 'root' ssh [email protected]
注意要把它放到 /bin/下面 然後賦予權限 并且是 chmod +x jump.sh
安裝 lrzsz 和 配置 iterm2 詳細請看 我另一篇 文章 iterm2如何安裝rz sz