we first run expect int shell cmd
if we don't install expect
we can use this to install expect in ubuntu:
sudo apt-get install expect
fiileName:scp.exp
# fileName:scp.exp
#!/usr/bin/expect -f
#Progame:
#we copy today's wrf_data_*.dat from server
#History:
#2011/07/20 feng First release v1.0
set timeout -1
#we set password here
set passwd password***
#we use [exec date "+%Y%m%d"] to run shell cmd ,then the return value ,we put in DATE
set DATE [exec date "+%Y%m%d"]
#命令需要多个参数时
set LASTDATE [exec date "-d last-day" "+%Y%m%d"]
#where we put the file in local
set LOCALPATH /home/feng/..........
#给ssh运行进程加个壳,用来传递交互指令
#scp today's wrf_data_*.dat
spawn scp **@ip:~/SZMB_forecast/wrf_data_${DATE}*.dat ${LOCALPATH}
match_max 100000
#when we need to put Password:
expect -exact "Password:"
#we send passwd to
send -- "$passwd\r"
expect eof
#scp lastday's 23 's wrf_data_*.dat
spawn scp **@ip:~/SZMB_forecast/wrf_data_${LASTDATE}23.dat ${LOCALPATH}
match_max 100000
#when we need to put Password:
expect -exact "Password:"
#we send passwd to
send -- "$passwd\r"
expect eof
#when we finish copying ,exitexit
expect scp.exp