2014-10-30 61 views
0

我有一个shell脚本,它不断地将一些数据从一台服务器放到另一台服务器上。它工作正常,但我想让它更安全。所以此刻如果其他服务器由于密码被更改而拒绝了权限,则scipts会冻结。有没有这种可能性,如果发生这种情况,它只是忽略这条线,只是继续?当权限被拒绝时,scp shell会停止

inotifywait -m /srv/watchfolderfilme -e create -e moved_to | 
      while read path action file; do 
... 
sshpass -p "****" scp -r /srv/newtorrentfiles/* [email protected]:/srv/torrentfiles && rm -r /srv/newtorrentfiles/* 
done 

回答

4

scp不是解决您问题的最佳工具。

正如乔治所说,使用公钥与ssh是摆脱密码更改的最佳方式。

你也可以做的伎俩,像这样的rsync:

rsync -ahz --remove-source-files /srv/newtorrentfiles/ [email protected]:/srv/torrentfiles/ 

rsync -ahz /srv/newtorrentfiles/ [email protected]:/srv/torrentfiles/ && rm -r /srv/newtorrentfiles/* 

要确保一切都过去了像你想(做这个脚本更“安全”),如果脚本由于某种原因未经授权而失败,则可以向您发送电子邮件。

1

也许不是你要找的答案,但你为什么不使用SSH密钥?

更新后的脚本:

inotifywait -m /srv/watchfolderfilme -e create -e moved_to | 
      while read path action file; do 
... 
scp -r /srv/newtorrentfiles/* [email protected]:/srv/torrentfiles && rm -r /srv/newtorrentfiles/* 
done 

怎么办呢

[email protected]:~> ssh-keygen -t rsa 
Generating public/private rsa key pair. 
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'. 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/a/.ssh/id_rsa. 
Your public key has been saved in /home/a/.ssh/id_rsa.pub. 
The key fingerprint is: 
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 [email protected] 

现在使用ssh上B.创建一个目录的〜/ .ssh为用户B(该目录可能已经存在,这很好):

[email protected]:~> ssh [email protected] mkdir -p .ssh 
[email protected]'s password: 

最后追加一个新的公钥给b @ B :的.ssh/authorized_keys文件,并输入B的密码是最后一次:

[email protected]:~> cat .ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys' 
[email protected]'s password: 

从现在起,你可以作为一个为B从A登录到B,不含密码:

[email protected]:~> ssh [email protected] 

来源>>http://www.linuxproblem.org/art_9.html