博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

scp命令写成脚本

Posted on 2013-07-24 19:58  david2303  阅读(873)  评论(0编辑  收藏  举报

 

scp.sh

#!/usr/bin/expect -f
set port 1234
set user "me"
set host "192.168.1.2"
set password "whatismypassword"
set timeout - 1
set file [ lindex $argv 0]
set hostFilePath "/home/me/"
spawn scp -P$port $file $user@$host:$hostFilePath
expect "*assword:*"

send "$password\r"
expect eof

 

每次敲scp命令,蛋疼的用户名,蛋疼的超复杂的密码,蛋疼的长长的路径。写成这样,自动执行,超爽
 
./scp.sh text.txt
就把test.txt传到远程的机器
 
expect,传参的话,用[lindex $argv 0],这里的0表示text.txt。刚开始还以为是1,所以一直取不到
spawn是expect的内建命令,需要chmod 755 scp.sh