通过expect实现scp密码自动输入

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



we run this by type cmd in shell:

expect scp.exp

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值