linux shell,linuxshell

來源:互聯網
上載者:User

【轉】linux shell,linuxshell

1、使用expect前,需要先安裝兩個rpm包,下載:http://download.csdn.net/detail/wang7dao/4416172
# rpm -ihv expect-5.43.0-8.el5.i386.rpm
# rpm -ihv expect-devel-5.43.0-8.el5.i386.rpm


2、使用指令檔的例子--實現自動輸密碼
#!/usr/bin/expect -f
set password 123456
#download
spawn scp root@192.168.1.218:/root/a.wmv /home/yangyz/
set timeout 300 
expect "root@192.168.1.218's password:"
set timeout 300 
send "$password\r"
set timeout 300 
send "exit\r"
expect eof 


3、在sh指令碼中嵌入expect的例子--通過連上一個公網的伺服器再轉跳到一個內網的伺服器上,用指令碼實現不用輸密碼,直接使用./goto.sh servername
#!/bin/bash
passmsmallq10="a"
passzhsh="a"
passfcwr="b"
passwapfx="c"
passadfx="d"


ip1="200.100.10.10"
ip2="10.100.100.70"
ip3="10.100.100.60"
ip4="10.100.100.10"
ip5="10.100.100.20"


case $1 in 
"zhsh") passstr=$passzhsh ipstr=$ip2 ;;
"fcwr") passstr=$passfcwr ipstr=$ip3 ;;
"wapfx") passstr=$passwapfx ipstr=$ip4 ;;
"adfx") passstr=$passadfx ipstr=$ip5 ;;
*) echo "The parameter $1 isn't exist"
exit 0 ;;
esac


command1="ssh -l m_smallq -p 36000 $ip1"
command2="ssh -l mqq -p 36000 $ipstr"


expect -c "
        set timeout 60;
        spawn $command1;
        expect {
                \"221.130.15.10's password:\" {send \"$passmsmallq10\r\"; exp_continue}
                \"m_smallq\" {send \"$command2\r\"; exp_continue}
                \"mqq's password:\" {send \"$passstr\r\";interact}
                }
        "


對上面例子的expect的解說
expect -c "..."  --裡面輸入命令
expect {...}     --裡面的多行記錄,從上向下掃描匹配,誰先匹配誰先處理。


4、ssh到另一台機子執行df -h後退出,要點是send後面可以跟多個命令,通過\r來分行成多個命令
#!/bin/bash
ip1="183.62.178.191"
command1="ssh -l root -p 14322 $ip1"


expect -c "
        spawn $command1;
        expect {
                \"183.62.178.191's password:\" {send \"aa\r\"; exp_continue}
                \"root@\" {send \"df -h\r exit\r\"; exp_continue}
                }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.