linux 自動上傳程式linux下expect安裝

來源:互聯網
上載者:User
linux下expect安裝

expect據說是互動性很強的指令碼語言,想安裝了玩玩試試,沒想到竟然讓我糾結兩天才安裝上,只是因為expect依賴於tcl,但linux系統裡一般不內建安裝tcl,需要手動安裝

expect版本 5.43

http://download.chinaunix.net/download/0003000/2845.shtml

tcl版本 8.4.19

http://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz/download

下載兩個包,分別解壓

1。先安裝tcl

  進入tcl解壓目錄,然後進入unix目錄

  #./configure

  #make

  #make install

2.後安裝expect

  進入expect解壓目錄

  #./configure --with-tclinclude=/usr/src/tcl8.4.19/generic/ --with-tclconfig=/usr/local/lib/

  #make

  #make install

完成,測試

  #expect

  expect1.1> 
  expect1.1>

一切OK

 

 

2.升級指令碼

 

對之前的版本進行升級,增加支援密碼從命令列輸入方式,增加提示那些機器上傳成功或者失敗提示
對於營運來說,同時管理多台機器是很辛苦的事情,特別是CDN營運需要上傳一個檔案到1000台機器的話,靠人工一個個上傳非常費勁,為此我寫了一個批量scp檔案到多台機器上的小程式。
其中用到了expect:
  Expect在這個程式裡就是用來協助自動輸入scp的密碼,Expect主要用於把需要人工互動的程式變為程式自動化完成,這個對於營運批量部署系統,批量無人值守安裝,批量執行命令,批量上傳下載
 現代的Shell對程式提供了最小限度的控制(開始,停止,等等),而把互動的特性留給了使用者。 這意味著有些程式,你不能非互動的運行,比如說passwd。 有一些程式可以非互動的運行,但在很大程度上喪失了靈活性,比如說fsck。這表明Unix的工具構造邏輯開始出現問題。Expect恰恰填補了其中的一些裂痕,解決了在Unix環境中長期存在著的一些問題。

 
  Expect使用Tcl作為語言核心。不僅如此,不管程式是互動和還是非互動的,Expect都能運用。

 
1.multi_scp_upload.sh的原始碼
 
#!/bin/bash  
#author: yifangyou  
#create time:2011-05-17  
#用來通過scp批量上傳檔案或者目錄到目標機器的指定目錄  
#設定檔格式:  
#ssh_hosts=("1.1.1.1" "2.2.2.2")  
#ssh_ports=("22" "22") 這個可以預設,預設值為22,或者個數比ssh_hosts少時,使用預設值  
#ssh_users=("root" "root") 這個可以預設,預設值為root,,或者個數比ssh_hosts少時,使用預設值  
#ssh_passwords=("323" "222") 這個可以預設,預設的話需要從命令列輸入,或者個數比ssh_hosts少時,使用命令列輸入  
#執行:sh multi_scp.sh conf_file_path file target  
if [ -z "$3" ]  
then 
echo "sh multi_scp.sh conf_file_path file target";  
exit;  
fi  
default_ssh_user="root" 
default_ssh_port="22";  
#upload shell script file path  
scp_upload=scp_upload.sh  
#configure file path  
conf_file=$1  
#then upload file path  
scp_file=$2  
#remote hosttarget file or dir path  
scp_target=$3  
#判斷conf_file設定檔是存在  
if [ ! -e "$conf_file" ]  
then 
echo "$conf_file is not exists";  
exit;  
fi  
#判斷scp_file是檔案或者目錄  
if [ ! -e "$scp_file" ] && [ ! -d "$scp_file" ]  
then 
echo "$scp_file is not exists";  
exit;  
fi  
#read configure file  
source $conf_file  
#若是沒有在設定檔裡提供密碼,則在命令列輸入  
if [ "${#ssh_passwords[@]}" = "0" ] || [ "${#ssh_passwords[@]}" -lt "${#ssh_hosts[@]}" ]  
then 
read -p "please input password:" -s default_ssh_password  
fi  
success_hosts="";  
fail_hosts="";  
for((i=0;i<${#ssh_hosts[@]};i++))  
do  
#remote ssh host  
ssh_host=${ssh_hosts[$i]};  
#remote ssh port  
ssh_port=${ssh_ports[$i]};  
if [ "$ssh_port" = "" ]  
then 
ssh_port=$default_ssh_port;  
fi  
#remote ssh user 
ssh_user=${ssh_users[$i]};  
if [ "$ssh_user" = "" ]  
then 
ssh_user=$default_ssh_user;  
fi  
#remote ssh password 
ssh_password=${ssh_passwords[$i]};  
if [ "$ssh_password" = "" ]  
then 
ssh_password=$default_ssh_password;  
fi  
echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) start" 
#scp file or dir  
/usr/bin/expect scp_upload.sh "$ssh_host" "$ssh_port" "$ssh_user" "$ssh_password" "$scp_file" "$scp_target" 
if [ "$?" -eq "0" ]  
then 
success_hosts="$success_hosts,$ssh_host" 
else 
fail_hosts="$fail_hosts,$ssh_host" 
fi  
echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) end" 
echo "" 
done  
echo "success_hosts=[$success_hosts]" 
echo "fail_hosts=[$fail_hosts]" 
2.scp_upload.sh的原始碼
 
#!/usr/bin/expect  
#author: yifangyou  
#create time:2011-05-17  
#host  
set scphost "[lindex $argv 0]" 
#ssh連接埠  
set port "[lindex $argv 1]" 
#ssh使用者名稱  
set scpuser "[lindex $argv 2]" 
#ssh密碼  
set scppw "[lindex $argv 3]" 
#要上傳的檔案名稱或者目錄  
set file "[lindex $argv 4]" 
#要上傳到遠程機器的檔案名稱或者目錄  
set target "[lindex $argv 5]" 
spawn scp -r -P $port $file $scpuser@$scphost:$target  
#設定逾時時間,防止遠程機器防火牆沒有開,而掛起  
set timeout 30  
expect {  
#respose: "root@1.2.3.4s password:",自動輸入密碼  
"*password*" {  
set timeout 30  
send "$scppw " 
}  
#the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes" 
"*yes*" {  
set timeout 30  
send "yes " 
set timeout 30  
expect "*password*" 
set timeout 30  
send "$scppw " 
}  
busy {send_user " <error:busy>";exit 1;}  
failed {send_user " <error:failed>";exit 2;}  
timeout {send_user " <error:timeout>";exit 3;}  
}  
#Permission denied not try again,回報出錯資訊  
set timeout 30  
expect {  
"*denied*" {  
send_user " <error:Permission denied>" 
exit 4  
}  
"*No such file*" {  
send_user " <error:No such file>" 
exit 5  
}  
busy {send_user " <error:busy>";exit 6;}  
failed {send_user " <error:failed>";exit 7;}  
timeout {send_user " <error:timeout>";exit 8;}  
}  
exit 0 
3.設定檔格式scp.conf
 
#ssh_hosts=("1.1.1.1" "2.2.2.2")  
#ssh_ports=("22" "22") #wheen port_num < host_num use default=22,or ssh_ports is undefined use 22 as default value  
#ssh_users=("root" "root") #wheen user_num < host_num use default=root,or ssh_users is undefined use root as default value  
#ssh_passwords=("323" "222") #wheen password_num < host_num use default=input password,or ssh_users is undefined use input password 
4.運行代碼
找一台機器可以和要上傳的機器聯通,安裝好expect(可以用expect命令測試是否已經安裝過了)

把scp_upload.sh,multi_scp_upload.sh,scp.conf放到同一個目錄下,運行multi_scp_upload.sh即可 

expect據說是互動性很強的指令碼語言,想安裝了玩玩試試,沒想到竟然讓我糾結兩天才安裝上,只是因為expect依賴於tcl,但linux系統裡一般不內建安裝tcl,需要手動安裝

expect版本 5.43

http://download.chinaunix.net/download/0003000/2845.shtml

tcl版本 8.4.19

http://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz/download

下載兩個包,分別解壓

1。先安裝tcl

  進入tcl解壓目錄,然後進入unix目錄

  #./configure

  #make

  #make install

2.後安裝expect

  進入expect解壓目錄

  #./configure --with-tclinclude=/usr/src/tcl8.4.19/generic/ --with-tclconfig=/usr/local/lib/

  #make

  #make install

完成,測試

  #expect

  expect1.1> 
  expect1.1>

一切OK

 

 

2.升級指令碼

 

對之前的版本進行升級,增加支援密碼從命令列輸入方式,增加提示那些機器上傳成功或者失敗提示
對於營運來說,同時管理多台機器是很辛苦的事情,特別是CDN營運需要上傳一個檔案到1000台機器的話,靠人工一個個上傳非常費勁,為此我寫了一個批量scp檔案到多台機器上的小程式。
其中用到了expect:
  Expect在這個程式裡就是用來協助自動輸入scp的密碼,Expect主要用於把需要人工互動的程式變為程式自動化完成,這個對於營運批量部署系統,批量無人值守安裝,批量執行命令,批量上傳下載
 現代的Shell對程式提供了最小限度的控制(開始,停止,等等),而把互動的特性留給了使用者。 這意味著有些程式,你不能非互動的運行,比如說passwd。 有一些程式可以非互動的運行,但在很大程度上喪失了靈活性,比如說fsck。這表明Unix的工具構造邏輯開始出現問題。Expect恰恰填補了其中的一些裂痕,解決了在Unix環境中長期存在著的一些問題。

 
  Expect使用Tcl作為語言核心。不僅如此,不管程式是互動和還是非互動的,Expect都能運用。

 
1.multi_scp_upload.sh的原始碼
 
#!/bin/bash  
#author: yifangyou  
#create time:2011-05-17  
#用來通過scp批量上傳檔案或者目錄到目標機器的指定目錄  
#設定檔格式:  
#ssh_hosts=("1.1.1.1" "2.2.2.2")  
#ssh_ports=("22" "22") 這個可以預設,預設值為22,或者個數比ssh_hosts少時,使用預設值  
#ssh_users=("root" "root") 這個可以預設,預設值為root,,或者個數比ssh_hosts少時,使用預設值  
#ssh_passwords=("323" "222") 這個可以預設,預設的話需要從命令列輸入,或者個數比ssh_hosts少時,使用命令列輸入  
#執行:sh multi_scp.sh conf_file_path file target  
if [ -z "$3" ]  
then 
echo "sh multi_scp.sh conf_file_path file target";  
exit;  
fi  
default_ssh_user="root" 
default_ssh_port="22";  
#upload shell script file path  
scp_upload=scp_upload.sh  
#configure file path  
conf_file=$1  
#then upload file path  
scp_file=$2  
#remote hosttarget file or dir path  
scp_target=$3  
#判斷conf_file設定檔是存在  
if [ ! -e "$conf_file" ]  
then 
echo "$conf_file is not exists";  
exit;  
fi  
#判斷scp_file是檔案或者目錄  
if [ ! -e "$scp_file" ] && [ ! -d "$scp_file" ]  
then 
echo "$scp_file is not exists";  
exit;  
fi  
#read configure file  
source $conf_file  
#若是沒有在設定檔裡提供密碼,則在命令列輸入  
if [ "${#ssh_passwords[@]}" = "0" ] || [ "${#ssh_passwords[@]}" -lt "${#ssh_hosts[@]}" ]  
then 
read -p "please input password:" -s default_ssh_password  
fi  
success_hosts="";  
fail_hosts="";  
for((i=0;i<${#ssh_hosts[@]};i++))  
do  
#remote ssh host  
ssh_host=${ssh_hosts[$i]};  
#remote ssh port  
ssh_port=${ssh_ports[$i]};  
if [ "$ssh_port" = "" ]  
then 
ssh_port=$default_ssh_port;  
fi  
#remote ssh user 
ssh_user=${ssh_users[$i]};  
if [ "$ssh_user" = "" ]  
then 
ssh_user=$default_ssh_user;  
fi  
#remote ssh password 
ssh_password=${ssh_passwords[$i]};  
if [ "$ssh_password" = "" ]  
then 
ssh_password=$default_ssh_password;  
fi  
echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) start" 
#scp file or dir  
/usr/bin/expect scp_upload.sh "$ssh_host" "$ssh_port" "$ssh_user" "$ssh_password" "$scp_file" "$scp_target" 
if [ "$?" -eq "0" ]  
then 
success_hosts="$success_hosts,$ssh_host" 
else 
fail_hosts="$fail_hosts,$ssh_host" 
fi  
echo "["`date +"%F %T"`"] (scp -r $scp_file $ssh_user@$ssh_host:$ssh_port:$scp_target) end" 
echo "" 
done  
echo "success_hosts=[$success_hosts]" 
echo "fail_hosts=[$fail_hosts]" 
2.scp_upload.sh的原始碼
 
#!/usr/bin/expect  
#author: yifangyou  
#create time:2011-05-17  
#host  
set scphost "[lindex $argv 0]" 
#ssh連接埠  
set port "[lindex $argv 1]" 
#ssh使用者名稱  
set scpuser "[lindex $argv 2]" 
#ssh密碼  
set scppw "[lindex $argv 3]" 
#要上傳的檔案名稱或者目錄  
set file "[lindex $argv 4]" 
#要上傳到遠程機器的檔案名稱或者目錄  
set target "[lindex $argv 5]" 
spawn scp -r -P $port $file $scpuser@$scphost:$target  
#設定逾時時間,防止遠程機器防火牆沒有開,而掛起  
set timeout 30  
expect {  
#respose: "root@1.2.3.4s password:",自動輸入密碼  
"*password*" {  
set timeout 30  
send "$scppw " 
}  
#the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes" 
"*yes*" {  
set timeout 30  
send "yes " 
set timeout 30  
expect "*password*" 
set timeout 30  
send "$scppw " 
}  
busy {send_user " <error:busy>";exit 1;}  
failed {send_user " <error:failed>";exit 2;}  
timeout {send_user " <error:timeout>";exit 3;}  
}  
#Permission denied not try again,回報出錯資訊  
set timeout 30  
expect {  
"*denied*" {  
send_user " <error:Permission denied>" 
exit 4  
}  
"*No such file*" {  
send_user " <error:No such file>" 
exit 5  
}  
busy {send_user " <error:busy>";exit 6;}  
failed {send_user " <error:failed>";exit 7;}  
timeout {send_user " <error:timeout>";exit 8;}  
}  
exit 0 
3.設定檔格式scp.conf
 
#ssh_hosts=("1.1.1.1" "2.2.2.2")  
#ssh_ports=("22" "22") #wheen port_num < host_num use default=22,or ssh_ports is undefined use 22 as default value  
#ssh_users=("root" "root") #wheen user_num < host_num use default=root,or ssh_users is undefined use root as default value  
#ssh_passwords=("323" "222") #wheen password_num < host_num use default=input password,or ssh_users is undefined use input password 
4.運行代碼
找一台機器可以和要上傳的機器聯通,安裝好expect(可以用expect命令測試是否已經安裝過了)

把scp_upload.sh,multi_scp_upload.sh,scp.conf放到同一個目錄下,運行multi_scp_upload.sh即可 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.