這篇文章主要介紹“l(fā)inux下expect環(huán)境的安裝以及簡單腳本測試”,在日常操作中,相信很多人在linux下expect環(huán)境的安裝以及簡單腳本測試問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”linux下expect環(huán)境的安裝以及簡單腳本測試”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)城口免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。一、安裝expect和tcl
(1)、解壓tcl,進(jìn)入tcl解壓目錄,然后進(jìn)入unix目錄進(jìn)行編譯安裝
[root@slient tmp]# tar -xzvf tcl8.4.11-src.tar.gz
..................................
[root@slient tmp]# cd tcl8.4.11/unix/
[root@slient unix]# ./configure
..................................
[root@slient unix]# make && make install
..................................
(2)、安裝expect
[root@slient tmp]# tar -xzvf expect-5.43.0.tar.gz
..................................
[root@slient tmp]# cd expect-5.43
[root@slient expect-5.43]# ./configure --with-tclinclude=/tmp/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/
..................................
[root@slient expect-5.43]# make && make install
..................................
(3)、安裝完成后進(jìn)行測試
[root@slient expect-5.43]# expect
expect1.1>
expect1.1>
二、下面結(jié)合shell腳本做簡單測試
示例:從本機(jī)自動(dòng)登錄到遠(yuǎn)程機(jī)器192.168.56.12(端口是22,密碼是:oracle)
登錄到遠(yuǎn)程機(jī)器后做以下幾個(gè)操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit自動(dòng)退出
[root@slient ~]# cat test-ssh.sh
#!/bin/bash
passwd='oracle'
/usr/local/bin/expect <<-EOF
set time 30
spawn ssh -p22 root@192.168.56.12
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$passwd\r" }
}
expect "*#"
send "useradd wangshibo\r"
expect "*#"
send "mkdir /opt/test\r"
expect "*#"
send "exit\r"
interact
expect eof
EOF
[root@slient ~]#
--執(zhí)行:
[root@slient ~]# sh test-ssh.sh
spawn ssh -p22 root@192.168.56.12
The authenticity of host '192.168.56.12 (192.168.56.12)' can't be established.
RSA key fingerprint is 16:8d:5a:fb:f2:58:e1:ee:4c:98:3d:76:ec:48:bb:46.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.12' (RSA) to the list of known hosts.
root@192.168.56.12's password:
Last login: Tue Jan 30 15:58:08 2018 from 192.168.56.1
[root@one ~]# useradd wangshibo
[root@one ~]# mkdir /opt/test
[root@one ~]#
[root@slient ~]#
[root@slient ~]#
上面的例子如果只是自動(dòng)登陸,登陸機(jī)器后不做操作的腳本內(nèi)容如下:
shell腳本的寫法:
[root@slient ~]# cat test.sh
#!/bin/bash
passwd='oracle'
/usr/local/bin/expect <<-EOF
set time 30
spawn ssh -p22 root@192.168.56.12
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$passwd\r" }
}
expect eof
EOF
[root@slient ~]#
--執(zhí)行:
[root@slient ~]# sh test.sh
spawn ssh -p22 root@192.168.56.12
root@192.168.56.12's password:
Last login: Tue Jan 30 16:03:23 2018 from 192.168.56.20
[root@one ~]#
expect腳本的寫法:
[root@slient ~]# cat test
#!/usr/local/bin/expect
set timeout 30
spawn ssh -p22 root@192.168.56.12
expect "*password:"
send "oracle\r"
interact
[root@slient ~]#
--執(zhí)行:
[root@slient ~]# ./test
spawn ssh -p22 root@192.168.56.12
root@192.168.56.12's password:
Last login: Tue Jan 30 16:07:23 2018 from 192.168.56.20
[root@one ~]#
注意:spawn后面跟的是操作動(dòng)作,比如登陸機(jī)器后執(zhí)行uptime,即:spawn ssh -p22 root@192.168.1.201 "uptime"
三、示例使用expetc自動(dòng)化傳送文件到遠(yuǎn)程主機(jī)目錄上
[oracle@slient ~]$ cat sftp.sh
#!/usr/local/bin/expect
expect<
spawn sftp oracle@192.168.56.12
expect "password:"
send "oracle\n";
expect "sftp>"
send "lcd /home/oracle/dmp\n";
expect "sftp>"
send "cd /home/oracle/soft\n";
expect "sftp>"
send "put tb_pt.dmp\n";
expect "sftp>"
send "exit\n"
interact
!
[oracle@slient ~]$
[oracle@slient ~]$ chmod +x sftp.sh
--執(zhí)行:
[oracle@slient ~]$ sh sftp.sh
spawn sftp oracle@192.168.56.12
Connecting to 192.168.56.12...
oracle@192.168.56.12's password:
sftp> lcd /home/oracle/dmp
sftp> cd /home/oracle/soft
sftp> put tb_pt.dmp
Uploading tb_pt.dmp to /home/oracle/soft/tb_pt.dmp
tb_pt.dmp 100% 216KB 216.0KB/s 00:00
sftp> [oracle@slient ~]$
[oracle@slient ~]$
--驗(yàn)證:
[oracle@one soft]$ pwd
/home/oracle/soft
[oracle@one soft]$
[oracle@one soft]$ ls
[oracle@one soft]$ pwd
/home/oracle/soft
[oracle@one soft]$ ls
tb_pt.dmp
[oracle@one soft]$
四、示例解釋
例子:
#!/usr/bin/expect
#set timeout 20 #設(shè)置超時(shí)時(shí)間
spawn ssh root@192.168.43.131
expect "*password:"
send "123\r"
# expect "*#"
interact
解釋:
1.#!/usr/bin/expect :需要先安裝軟件,然后來說明用expect來執(zhí)行
2.spawn ssh root@192.168.43.131 :spawn是進(jìn)入expect環(huán)境后才可以執(zhí)行的expect內(nèi)部命令,用來執(zhí)行它后面的命令。
3.expect "*password:" :也是expect的內(nèi)部命令,用來解惑關(guān)鍵的字符串,如果有,就會立即返回下面設(shè)置的內(nèi)容,如果沒有就看是否設(shè)置了超時(shí)時(shí)間。
4.send "123\r":這時(shí)執(zhí)行交互式動(dòng)作,與手工輸入密碼等效,在expect截獲關(guān)鍵字之后,它就會輸入send后面的內(nèi)容。
5.interact :執(zhí)行完畢后把持交互狀態(tài),把控制臺,這時(shí)候就可以進(jìn)行你想要進(jìn)行的操作了。如果沒有這一句,在登陸完成之后就會退出,而不是留在遠(yuǎn)程終端上。
到此,關(guān)于“l(fā)inux下expect環(huán)境的安裝以及簡單腳本測試”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!