我有以下腳本:傳遞變量到rsync的
#!/bin/sh
...
rsync -e 'ssh -i "$SSHKeyPath"'
的錯誤是:
Warning: Identity file $SSHKeyPath not accessible: No such file or directory.
我怎樣才能$ SSHKeyGen的rsync之前評估被調用?
更新: fwiw,這是在OSX上。
我有以下腳本:傳遞變量到rsync的
#!/bin/sh
...
rsync -e 'ssh -i "$SSHKeyPath"'
的錯誤是:
Warning: Identity file $SSHKeyPath not accessible: No such file or directory.
我怎樣才能$ SSHKeyGen的rsync之前評估被調用?
更新: fwiw,這是在OSX上。
有幾種解決方案,我認爲是比較容易:
ssh-agent(1)
解開密鑰的私有部分爲ssh(1)
過程,因爲他們需要它。這是迄今爲止最容易使用的機制。使用~/.ssh/config
基於主機名來選擇不同的私鑰:
host backuphost
IdentityFile ~/.ssh/different_key
那麼就沒有必要指定命令行上的一個鍵。
既然你想獨立於個人用戶的關鍵更新,這使得很多更有意義,我現在。如果您在使用sh
另一個變量可以使你原有的工作方式:
$ cat foo.sh
#!/bin/sh
SSHKeyPath=/home/sarnold/.ssh/id_rsa
KEYARG="ssh -i $SSHKeyPath"
rsync -e "$KEYARG" /tmp/pointless localhost:/tmp/new_pointless
$ ./foo.sh
Enter passphrase for key '/home/sarnold/.ssh/id_rsa':
skipping directory pointless
您使用單引號,這是不能代替變量名與它的價值。這與雙倍報價是一致的。
例如
$> path_to_file="/tmp/file"; rsync -e "$(ssh -i "$path_to_file")"
Warning: Identity file /tmp/file not accessible: No such file or directory.
我認爲你的'$(...)'拋出瞭解決方案 - 它正在執行'ssh',而不是讓'rsync'執行'ssh'。嘗試用'echo hello'替換'rsync -e' - 即使它不應該被執行,你仍然會看到'ssh'的輸出。 – sarnold
這個解決方案導致'usage:ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]' – eatloaf
看到我上面關於在測試中使用'echo(1)'的危險的有趣評論。呵呵。但是,仍然可以通過主機和路徑完全嘗試,並且我認爲你會看到'ssh(1)'不能正常工作。 – sarnold
試試這個:
rsync -e "ssh -i ""$SSHKeyPath"
不幸的是,目標是腳本可移植。我認爲使用這些方法需要在最終用戶的機器上添加更多的代碼。 – eatloaf
第二種解決方案對我來說很有意義,但是我在遠程shell命令中得到了'丟失的尾部'。' – eatloaf
哦,我今晚學到了一個很好的教訓:'echo(1)'的輸出並不總是值得信賴。它可能會像'ssh -i path''一樣,但是當它通過'rsync(1)'傳遞給'execve(2)'時,它被解析成:'execve(「/ usr/bin/rsync」, [「rsync」,「-e」,「ssh」,「-i」,「/home/s...rsa'」,「/ tmp/...」...)'。顯然,shell引用規則比我想起的更加微妙。用實際的'rsync(1)'測試讓我得到一個實際的工作版本,如上所述。 – sarnold