標籤:style color os 使用 ar 檔案 sp div on
通過在shell指令碼中用expect實現遠程scp檔案: 使用expect前,需要先安裝兩個rpm包:# rpm -ihv CentOS/expect-5.43.0-5.1.i386.rpm
# rpm -ihv CentOS/expect-devel-5.43.0-5.1.i386.rpm 或者yum直接下載安裝yum install expect -y 指令碼如下:
#!/usr/bin/expect -f
set password 密碼
spawn scp 使用者名稱@目標機器ip:拷貝檔案的路徑 存放本地檔案的路徑
set timeout 300
expect "使用者名稱@目標機器ip‘s password:" //注意:這裡的“使用者名稱@目標機器ip” 跟上面的一致
set timeout 300
send "$password\r"
set timeout 300 //此處設定逾時時間,單位為秒,如果,拷貝檔案比較大並且多時,應該將這個值調大一些。
send "exit\r"expect eof附:scp參數
-r:拷貝目錄
-c:允許壓縮一個完整的例子
expect_scp.exp
========================================================================
- #!/usr/bin/expect -f
- #Author by kevin
- #date is 2011-11-14
- set password vcdog
- #download local host
- spawn scp -r [email protected].168.1.107:/root/dba_tool/test /root/DBA_TOOL/
- set timeout 3
- expect {
- "yes/no" {send "yes\r";exp_continue}
- }
- expect "[email protected]‘s password:"
- set timeout 3
- send "$password\r" ----或者\n
- set timeout 300
- send "exit\r"
- expect eof
- #upload remote host
- spawn scp -r /root/DBA_TOOL/t.txt [email protected].168.1.107:/root/dba_tool/
- set timeout 3
- expect {
- "yes/no" {send "yes\r";exp_continue}
- }
- expect "[email protected]‘s password:"
- set timeout 3
- send "$password\r"
- set timeout 300
- send "exit\r"
- expect eof
==========================================================================測實驗證:
# chmod +x ./expect_scp.exp
# expect ./expect_scp.expspawn scp -r [email protected]:/root/dba_tool/test /root/DBA_TOOL/
[email protected]‘s password:
test 100% 12 0.0KB/s 00:00
spawn scp -r /root/DBA_TOOL/t.txt [email protected]:/root/dba_tool/
[email protected]‘s password:
t.txt 100% 254 0.3KB/s 00:00 本機伺服器:(192.168.1.106)[[email protected] DBA_TOOL]# ll /root/DBA_TOOL/
total 64
-rw-r--r-- 1 root root 548 Nov 14 23:46 expect_scp.exp
-rw-r--r-- 1 root root 12 Nov 14 23:24 test
-rw-r--r-- 1 root root 254 Nov 14 23:24 t.txt
遠程伺服器:(192.168.1.107)
[[email protected] dba_tool]# ll /root/dba_tool/
total 60
-rw-r--r-- 1 root root 12 Nov 14 22:24 test
-rw-r--r-- 1 root root 254 Nov 14 22:47 t.txt
shell 自動登入 上傳 下載