在使用ssh登錄遠程服務器的時候,在執行完ssh user@ip后,要輸入登錄密碼,有時候登錄密碼記不住,這樣以來Ian帶來的很多的麻煩,有沒有一種在ssh的參數中直接加入密碼的方法呢?查看ssh的幫助我們發現ssh命令並不能在參數中制定密碼。
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname [command]
於是各種google,找到sshpass
sshpass:用於非交互的ssh 密碼驗證,允許你用 -p 參數指定明文密碼,然后直接登錄遠程服務器。 它支持密碼從命令行,文件,環境變量中讀取。
首先在機器上安裝sshpass
對於debian/ubuntu系統來說,安裝方式很簡單:
sudo apt-get install sshpass
對於其他的linux,可以編譯sshpass的源碼安裝:
wget http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz tar xvzf sshpass-1.05.tar.gz ./configure make sudo make install
安裝好之后,使用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
於是把sshpass和ssh命令集合就能實現ssh登錄的時候加入密碼了,這樣把登錄某台計算機的命令寫成shell腳本,后面就十分的方便了
#!/bin/bash
sshpass -p "XXX" ssh user@IP