歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux命令expect實現ssh登陸

linux命令expect實現ssh登陸

日期:2017/3/1 12:22:25   编辑:關於Linux
1 expect可用於自動化腳本的書寫 yum -y install expect即可下載 2 腳本ssh.exp #此行用於解釋器,這樣意味著你可以./ssh.exp了,或者不寫這行直接/usr/bin/expect ssh.exp也行 #!/usr/bin/expect -f #設置參數的方法是使用set,如果想獲取命令行參數,則使用[ index $argv 0 ]表示獲取第一個參數 set ip "localhost" set passwd "liuliancao" set timeout 10 #生成一個進程 spawn ssh liuliancao@$ip expect "(yes/no)?" {send "yes\r";exp_continue} #如果遇到了(yes/no)?這樣的字符串就輸入yes和換行符號,繼續後面的expect,注意{前面有個空格,expect後面有個空格 expect "password:" {send "$passwd\r";interact} #如果遇到了password:這樣的字符串就輸入用戶秘密,並保持交互 3 腳本ssh_without_passwd.exp #!/usr/bin/expect -f set ip [ lindex $argv 0 ] set passwd [ lindex $argv 1 ] spawn ssh-copy-id -i /root/.ssh/id_rsa root@$ip expect "yes/no" {send "yes\r";exp_continue} expect "password:" {send "$passwd\r"} interact
Copyright © Linux教程網 All Rights Reserved