亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

linux下expect環(huán)境安裝以及簡(jiǎn)單腳本測(cè)試

asal 2016-11-07 16:32:37 489
abstrak:expect是交互性很強(qiáng)的腳本語言,可以幫助運(yùn)維人員實(shí)現(xiàn)批量管理成千上百臺(tái)服務(wù)器操作,很實(shí)用!expect依賴于tcl,而linux系統(tǒng)里一般不自帶安裝tcl,所以需要手動(dòng)安裝下載:expect-5.43.0.tar和tcl8.4.11-src.tar下載地址:https://pan.baidu.com/s/1kVyeLt9 提取密碼:af9p將expect和tcl的軟件包下載放到/us

expect是交互性很強(qiáng)的腳本語言,可以幫助運(yùn)維人員實(shí)現(xiàn)批量管理成千上百臺(tái)服務(wù)器操作,很實(shí)用!
expect依賴于tcl,而linux系統(tǒng)里一般不自帶安裝tcl,所以需要手動(dòng)安裝

下載:expect-5.43.0.tar和tcl8.4.11-src.tar
下載地址:https://pan.baidu.com/s/1kVyeLt9 
提取密碼:af9p

將expect和tcl的軟件包下載放到/usr/local/src目錄下

(1)解壓tcl,進(jìn)入tcl解壓目錄,然后進(jìn)入unix目錄進(jìn)行編譯安裝
[root@xw4 src]# tar -zvxf tcl8.4.11-src.tar.gz
[root@xw4 src]# cd tcl8.4.11/unix
[root@xw4 unix]# ./configure
[root@xw4 unix]# make && make

(2)安裝expect
[root@xw4 src]# tar -zvxf expect-5.43.0.tar.gz
[root@xw4 src]# cd expect-5.43.0
[root@xw4 expect-5.43.0]# ./configure --with-tclinclude=/usr/local/src/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/
[root@xw4 expect-5.43.0]# make && make install

(3)安裝完成后進(jìn)行測(cè)試
[root@xw4 ~]# expect
expect1.1> 
expect1.1>

----------------------------------------------------------------------------------------------------

下面結(jié)合shell腳本做簡(jiǎn)單測(cè)試:

例1:
從本機(jī)自動(dòng)登錄到遠(yuǎn)程機(jī)器192.168.1.200(端口是22,密碼是:PASSWORD)
登錄到遠(yuǎn)程機(jī)器后做以下幾個(gè)操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit自動(dòng)退出

[root@xw4 tmp]# cat test-ssh.sh 
#!/bin/bash
passwd='PASSWORD'
/usr/local/bin/expect <<-EOF
set time 30
spawn ssh -p22 root@192.168.1.201
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@xw4 tmp]# sh test.sh
spawn ssh -p22 root@192.168.1.201
root@192.168.1.201's password: 
Last login: Fri Sep 23 16:21:20 2016 from 192.168.1.23
[root@vm-002 ~]# useradd wangshibo
[root@vm-002 ~]# mkdir /opt/test
[root@vm-002 ~]# [root@xw4 tmp]#

******************************************************************************************************* 

例2:

我們?cè)诓渴馃o密碼訪問時(shí),手工建立ssh互信需要好幾個(gè)步驟,并且中途人工交互(輸入密碼等),如果機(jī)器數(shù)目多,則很繁瑣!

下面方法用于自動(dòng)化生成authorized_keys,免去了手工數(shù)據(jù).

方法: 利用expect編寫sshkey.exp在遠(yuǎn)程主機(jī)上生成id_rsa,并重定向到本地.在利用noscp.exp.把文件復(fù)制到遠(yuǎn)程主機(jī)
為了節(jié)省自己的時(shí)間,可以寫個(gè)expect自動(dòng)化腳本,分享如下:

(1)
如上expect安裝后的路徑是:
[root@xw4 ~]# which expect
/usr/local/bin/expect

(2)
做個(gè)expect執(zhí)行文件的軟件
[root@xw4 ~]# ln -s /usr/local/bin/expect /usr/bin/expect
[root@xw4 ~]# ll /usr/bin/expect

(3)
編寫expect腳本:
-----------------------------------------------------------------------------------
1)
[root@xw4 ~]# cat sshkey.exp 
#!/usr/bin/expect

#sshkey.exp

if {$argc<3} {
puts stderr "Usage: $argv0 host user passwd "
exit 1
}

set host [ lindex $argv 0 ]
set user [ lindex $argv 1 ]
set pwd [ lindex $argv 2 ]

set timeout 30

#spawn ssh ${user}@${host} "rm -rf ~/.ssh/id_rsa*"
#
#expect {
# "*yes/no" { send "yes\r"; exp_continue }
# "*password:" { send "$pwd\r"; exp_continue }
#}

spawn ssh ${user}@${host} "ssh-keygen -t rsa"

expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r"; exp_continue }
"Enter file in which to save the key*" { send "\n\r"; exp_continue }
"Overwrite*" { send "y\n"; exp_continue } 
"Enter passphrase (empty for no passphrase):" { send "\n\r"; exp_continue }
"Enter same passphrase again:" { send "\n\r" }
}

spawn ssh ${user}@${host} "cat ~/.ssh/id_rsa.pub"

expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r" }
}

expect eof

----------------------------------------------------------------------------------------------------
2)
[root@xw4 ~]# cat noscp.exp 
#!/usr/bin/expect

#noscp.exp

if {$argc<4} {
puts stderr "Usage: $argv0 localfile remotefile user passwd "
exit 1
}

set localfile [ lindex $argv 0 ]
set remotefile [ lindex $argv 1 ]
set user [ lindex $argv 2 ]
set pwd [ lindex $argv 3 ]

set timeout 30

spawn scp ${localfile} ${user}@${remotefile}

expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r" }
}

expect eof

------------------------------------------------------------------------

[root@xw4 ~]# chmod 755 sshkey.exp
[root@xw4 ~]# chmod 755 noscp.exp

(4)
腳本說明
./sshkey.exp 主機(jī)名 用戶名 密碼 (在遠(yuǎn)程主機(jī)生成id_rsa)
./noscp.exp 本地文件 遠(yuǎn)程路徑 遠(yuǎn)程用戶密碼 (無密碼拷貝文件)

(5)驗(yàn)證:
[root@xw4 ~]# ./sshkey.exp 192.168.1.201 root PASSWORD |grep ssh-rsa >> ~/.ssh/authorized_keys
[root@xw4 ~]# ./noscp.exp ~/.ssh/authorized_keys 192.168.1.201:~/.ssh root PASSWORD
spawn scp /root/.ssh/authorized_keys root@192.168.1.201:~/.ssh
root@192.168.1.201's password: 
authorized_keys

這樣,就能無密碼登陸了!
[root@xw4 ~]# ssh 192.168.1.201
Last login: Fri Sep 23 18:33:21 2016 from 192.168.1.7
[root@vm-002 ~]#

-----------------------------------
如果是多臺(tái)機(jī)器的話,可以結(jié)合shell腳本進(jìn)行批量執(zhí)行

[root@xw4 ~]# cat /root/ip.list
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
......
......

[root@xw4 ~]# cat sshkey.sh
#!/bin/bash
user='root'
password='PASSWORD'
for ip in `cat /root/ip.list`
do
/root/sshkey.exp $ip $user $password |grep ssh-rsa >> ~/.ssh/authorized_keys
/root/noscp.exp ~/.ssh/authorized_keys $user@$ip:~/.ssh root PASSWORD 
done


Nota Keluaran

Penyertaan Popular