您好,登錄后才能下訂單哦!
1、sshpass:
使用場景:
ssh登陸不能在命令行中指定密碼,sshpass 的出現,解決了這一問題,用于非交互的ssh 密碼驗證 它支持密碼從命令行,文件,環境變量中讀取。
安裝
[root@node6 ~]# yum install sshpass -y 已安裝: sshpass.x86_64 0:1.05-1.el6 完畢! [root@node6 ~]#
參數:
[root@node6 ~]# [root@node6 ~]# sshpass --help sshpass: invalid option -- '-' 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支持三種模式,密碼,文件,環境變量
案例:
簡單模式:(修改端口,主機互信) [root@node3 ~]# ssh root@192.168.1.221 -p21386 'ls' Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! root@192.168.1.221's password: node2 RPM-GPG-KEY-EPEL-6 [root@node3 ~]# #命令行下: [root@node3 ~]# sshpass -prenzhiyuan ssh root@192.168.1.221 -p21386 'ls' Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! node2 RPM-GPG-KEY-EPEL-6 [root@node3 ~]# #文件模式: [root@node3 ~]# cat renzhiyuan renzhiyuan [root@node3 ~]# sshpass -f renzhiyuan ssh root@192.168.1.221 -p21386 'ls' Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! node2 RPM-GPG-KEY-EPEL-6 [root@node3 ~]# #環境變量里面 [root@node3 ~]# cat /etc/profile.d/renzhiyuan.sh export SSHPASS="renzhiyuan" sshpass -e ssh root@192.168.1.221 -p21386 'ls' [root@node3 ~]# [root@node3 ~]# /etc/profile.d/renzhiyuan.sh Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! node2 RPM-GPG-KEY-EPEL-6 [root@node3 ~]#
2、expect:
使用場景:
通過Shell可以實現簡單的控制流功能,如:循環、判斷等。但是對于需要交互的場合則必須通過人工來干預,有時候我們可能會需要實現和交互程序如telnet服務器等進行交互的功能。
而expect是一個免費的編程工具語言,用來實現自動和交互式任務進行通信,而無需人的干預。
[root@node6 ~]# yum install expect -y 已安裝: expect.x86_64 0:5.44.1.15-5.el6_4 作為依賴被安裝: tcl.x86_64 1:8.5.7-6.el6 完畢! [root@node6 ~]#
案例:
2.1)ssh實現自動登錄,并停在登錄服務器上 yum install expect -y [root@node3 ~]# cat ssh.sh #!/usr/bin/expect -f set ip [lindex $argv 0 ] set password [lindex $argv 1 ] set timeout 20 spawn ssh -p21386 root@$ip expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$password\r" } } interact [root@node3 ~]# ./ssh.sh 192.168.1.221 renzhiyuan spawn ssh -p21386 root@192.168.1.221 Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! root@192.168.1.221's password: Last login: Wed Dec 7 16:43:27 2016 from 192.168.1.217 [root@node3 ~]# #!/usr/bin/expect -f set ip [lindex $argv 0 ] //接收第一個參數,并設置IP set password [lindex $argv 1 ] //接收第二個參數,并設置密碼 set timeout 10 //設置超時時間 spawn ssh root@$ip //發送ssh請滶 expect { //返回信息匹配 "*yes/no" { send "yes\r"; exp_continue} //第一次ssh連接會提示yes/no,繼續 "*password:" { send "$password\r" } //出現密碼提示,發送密碼 } interact //交互模式,用戶會停留在遠程服務器上面. 2、2)根據IP和密碼連接到不同的機器. [root@node3 ~]# ./ssh.sh spawn ssh -p21386 root@192.168.1.221 Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! root@192.168.1.221's password: Last login: Wed Dec 7 16:43:56 2016 from 192.168.1.217 [root@node3 ~]# 2.3)遠程登錄到服務器,并且執行命令,執行完后并退出 [root@node3 ~]# ./ssh.sh spawn ssh -p21386 root@192.168.1.221 Address 192.168.1.221 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT! root@192.168.1.221's password: Last login: Wed Dec 7 16:45:33 2016 from 192.168.1.217 [root@HYXD ~]# pwd /root [root@HYXD ~]# exit logout Connection to 192.168.1.221 closed. [root@node3 ~]#
3、問題:(能力有限,至今尋求幫助和研究都沒出來)
如果做的是有密碼的ssh互信,如何利用sshpass或者except解決密鑰密碼交互式問題?
3.1)#sshpass -p '密碼' ssh -p21345 -i renzhiyuan 用戶@ip (不可取)
2.2)except腳本居然沒能越過ssh密鑰的密碼。
歡迎大家各抒己見,互相學習進步。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。