您好,登錄后才能下訂單哦!
這篇文章主要介紹“l(fā)inux下expect環(huán)境的安裝以及簡(jiǎn)單腳本測(cè)試”,在日常操作中,相信很多人在linux下expect環(huán)境的安裝以及簡(jiǎn)單腳本測(cè)試問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”linux下expect環(huán)境的安裝以及簡(jiǎn)單腳本測(cè)試”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
一、安裝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)行測(cè)試
[root@slient expect-5.43]# expect
expect1.1>
expect1.1>
二、下面結(jié)合shell腳本做簡(jiǎn)單測(cè)試
示例:從本機(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腳本的寫(xiě)法:
[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腳本的寫(xiě)法:
[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
解釋?zhuān)?/strong>
1.#!/usr/bin/expect :需要先安裝軟件,然后來(lái)說(shuō)明用expect來(lái)執(zhí)行
2.spawn ssh root@192.168.43.131 :spawn是進(jìn)入expect環(huán)境后才可以執(zhí)行的expect內(nèi)部命令,用來(lái)執(zhí)行它后面的命令。
3.expect "*password:" :也是expect的內(nèi)部命令,用來(lái)解惑關(guān)鍵的字符串,如果有,就會(huì)立即返回下面設(shè)置的內(nèi)容,如果沒(méi)有就看是否設(shè)置了超時(shí)時(shí)間。
4.send "123\r":這時(shí)執(zhí)行交互式動(dòng)作,與手工輸入密碼等效,在expect截獲關(guān)鍵字之后,它就會(huì)輸入send后面的內(nèi)容。
5.interact :執(zhí)行完畢后把持交互狀態(tài),把控制臺(tái),這時(shí)候就可以進(jìn)行你想要進(jìn)行的操作了。如果沒(méi)有這一句,在登陸完成之后就會(huì)退出,而不是留在遠(yuǎn)程終端上。
到此,關(guān)于“l(fā)inux下expect環(huán)境的安裝以及簡(jiǎn)單腳本測(cè)試”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。