在很早之前我都研究過這塊,最終的方案其實有點差強人意,可以看我那篇博客,當時需要預先寫好腳本,通過調用scp2.sh
進行路徑名展開,對那時的我而言,能免密就已然足夠了
隨着我手中的服務器日益增多,而且scp
操作已不是固定的N條命令,此時的我需要再次尋找出路。上次的經歷讓我得出的重要結論就是:路徑名展開 是 shell 的特性,expect 沒有
,只要我在terminal
中輸入路徑名,它總會在執行腳本前展開,堵不如疏,展開就展開嘛,我在腳本中操作展開後的參數不就好了?問題迎刃而解
#!/bin/bash
secret="secret.conf"
remote=""
password=""
port=""
if [[ $1 =~ @ ]]; then
remote=$1
fi
if [[ ${*:$#:1} =~ @ ]]; then
remote=${*:$#:1}
fi
eval $(grep ${remote%:*} $secret | awk '{printf("password=\"%s\";port=%s",$2,$3)}')
expect -c "set timeout -1;
spawn scp -r -P $port ${*:1:$#-1} ${*:$#:1};
expect \"Password:\";
send \"$password\n\";
expect eof"
如果你對以上腳本不太理解,我覺得多插入些echo
打印會幫助到你
secret.conf
大致這樣配置
[email protected] 123456 22
secret
修改成絕對路徑,腳本命名爲scpue
放在/usr/local/bin
目錄中,之後就可以如下使用啦
scpue test* [email protected]:/tmp
scpue [email protected]:/tmp/test* .
注:
- 新版本MacOS輸入密碼提示是Password,老版本是password,反正首字母不是大寫就是小寫,按需適當修改
scpue
腳本 - 新版本MacOS默認使用
zsh
,老版本是bash
,zsh
執行scpue [email protected]:/tmp/test* .
會報如下錯誤zsh: no matches found: [email protected]:/tmp/test*
,此時可以爲參數加單引號