linux自動交互工具expect,tcl安裝和安裝包,以及自動互信腳本

linux自動交互工具expect,tcl安裝,以及自動互信腳本


工作中需要對幾十臺服務器做自動互信,無意中發現expect命令,研究一番。

在網上找了許多資料也沒有安裝成功,摸索着總算成功了。現分享如下。

expect命令在自動交互中很好用。


系統:虛擬機fedora14,32位。
[root@Linux ~]# uname -a
Linux Linux 2.6.35.6-45.fc14.i686 #1 SMP Mon Oct 18 23:56:17 UTC 2010 i686 i686 i386 GNU/Linux
安裝包:
-rw-r--r--.  1 root  root   525572 Aug  5 19:22 expect-5.43.0.tar.gz
-rw-r--r--.  1 root  root  3641017 Jul 23 13:28 tcl8.4.16-src.tar.gz
安裝步驟:
解壓安裝tcl:
tar zxvf tcl8.4.16-src.tar.gz;
[root@Linux ~]# cd tcl8.4.16/unix/
./configure
make
make install
cp tclUnixPort.h ../generic/


解壓安裝expect:
tar zxvf expect-5.43.0.tar.gz;
[root@Linux ~]# cd expect-5.43
[root@Linux expect-5.43]# ./configure --with-tcl=/usr/local/lib/ --with-tclinclude=/usr/log/tcl8.4.16/generic  --with-x=no
上面配置這句是關鍵。
按照自己的tcl安裝位置修改tcl=/usr/local/lib/ 
修改tclinclude=/root/tcl8.4.16/generic   這個目錄是上面安裝tcl文件夾的目錄。
make
make install
--建立連接。
ln -s  /usr/local/bin/expect /usr/bin/expect 
--定義別名。使用ep代替expect。
echo "alias ep='expect'" >>/root/.bashrc 


本步驟也在linux redhat5.5  64位系統中安裝成功。
[root@HN509-AQ-WEB log]# uname -a
Linux HN509-AQ-WEB 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

安裝包以及互信腳本詳細地址:

http://download.csdn.net/detail/network__yang/5886409

有什麼問題可以互相探討。

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

以下是自動互信腳本的思考步驟,僅供參考。互相學習。

先刪除以前的密匙。
[root@HN509-AQ-WEB .ssh]# rm -f ~/.ssh/id_rsa*
沒有重複文件的生成步驟:需要3次按鍵。
[root@HN509-AQ-WEB .ssh]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 1 
Enter passphrase (empty for no passphrase):              2
Enter same passphrase again:                                   3

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f2:94:41:fb:1b:26:f5:8e:43:05:0e:e0:47:ce:b4:e4 root@HN509-AQ-WEB


如果以前已經生成,則需要4不按鍵。
[root@HN509-AQ-WEB .ssh]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
88:b3:05:27:c3:9f:1d:a7:92:88:4b:83:b6:44:db:b8 root@HN509-AQ-WEB


此腳本只適用於,/root/.ssh/文件夾下有id_rsa和id_rsa.pub文件的情況。
[root@HN509-AQ-WEB log]# cat tmp.sh 
#!/usr/bin/expect
spawn ssh-keygen -b 1024 -t rsa
expect *key*
send \r
expect *verwrite*
send y\r
expect *passphrase*
send \r
expect *again:
send \r
interact
爲了適應所有情況。建議執行前,在腳本中把id_rsa和id_rsa.pub文件刪除。rm -f ~/.ssh/id_rsa*
先刪除以前的密匙。
[root@HN509-AQ-WEB .ssh]# rm -f ~/.ssh/id_rsa*
沒有重複文件的生成步驟:需要3次按鍵。
[root@HN509-AQ-WEB .ssh]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 1 
Enter passphrase (empty for no passphrase):              2
Enter same passphrase again:                             3
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f2:94:41:fb:1b:26:f5:8e:43:05:0e:e0:47:ce:b4:e4 root@HN509-AQ-WEB
執行腳本。
[root@HN509-AQ-WEB log]# cat tmp.sh 
#!/usr/bin/expect
spawn ssh-keygen -b 1024 -t rsa
expect *key*
send \r
expect *empty*
send \r
expect *again:
send \r
interact


傳輸公匙文件,這裏是直接傳輸~/.ssh/到對端。由於對端可能沒有~/.ssh/文件夾,還需要建,所以直接傳輸文件夾。


[root@HN509-AQ-WEB log]# cat tmp.10.7.1.29.sh 
#!/usr/bin/expect
spawn scp -r  /root/.ssh [email protected]:~/
expect *assword*
send \r
expect *assword*
send thinker\r
interact


如果是一臺沒有登陸過的服務器,在本機的know_hosts裏面由於沒有相應的公匙,所以會提示是否增加這個文件。
先刪除本機的know_hosts文件,
rm -f ~/.ssh/known_hosts
修改自動腳本。
spawn scp -r /root/.ssh [email protected]:~/
The authenticity of host '10.7.1.31 (10.7.1.31)' can't be established.
RSA key fingerprint is 94:91:b1:7c:77:54:b9:72:c8:57:ca:c8:ec:20:7f:8c.
Are you sure you want to continue connecting (yes/no)? 
Host key verification failed.


[root@HN509-AQ-WEB log]# cat tmp.10.7.1.29.sh 
#!/usr/bin/expect
spawn scp -r  /root/.ssh [email protected]:~/
expect *yes*
send yes\r
expect *assword*
send \r
expect *assword*
send thinker\r
interact
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章