【问题标题】:Shell script to copy one directory from one server to another without asking for password用于将一个目录从一台服务器复制到另一台服务器而不要求输入密码的 Shell 脚本
【发布时间】:2016-12-10 01:58:54
【问题描述】:

我想写一个 shell 脚本并把它放在一个 cron 中。这个 shell 脚本每天都会将一个特定目录从我的服务器复制到另一台服务器。所以,我不希望它提示输入密码。我可以在我的脚本中添加一些东西,这样它就不会每天都要求输入密码了吗?

【问题讨论】:

标签: shell unix


【解决方案1】:

你需要在你的 Unix Box 中使用无密码的 SSH 登录

下面的链接描述了如何设置无密码SSH登录

http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/

您可以使用 FTP 或 NDM 传输文件

这样你就可以达到你的要求了。

【讨论】:

  • 我可以使用 rsync 吗?因为我需要这个脚本每天运行,所以不是一次又一次地复制,它只会更新。如果这是可能的,那么解释一下如何??
  • 我从来没有使用过 rsync,因为我们每天都会收到新的文件,所以我们每天都复制
  • 使用这个脚本我可以实现我之前提到的:com="sshpass -p Password0 scp arul@172.25.184.93:/home/arul/test.sh。"评估 $com
  • 感谢分享
【解决方案2】:

使用下面的脚本,我可以实现我提到的:

#!/bin/bash com="sshpass -p Password0 scp arul@172.25.184.93:/home/arul/test.sh 。" 评估 $com

【讨论】:

    【解决方案3】:

    您也可以为此使用 RSA 密钥选项。使用 RSA 密钥,您可以在第一台服务器中授权您的第二台服务器。这是一次性操作。

     ssh-copy-id -i ~/.ssh/id_rsa.pub [Your 2nd server IP]
    

    例子:-

    [root@vasmon home]#  ssh-copy-id -i ~/.ssh/id_rsa.pub xxx.xxx.xxx.xxx
    root@xxx.xxx.xxx.xxx's password: 
    Now try logging into the machine, with "ssh 'xxx.xxx.xxx.xxx'", and check in:
    
    .ssh/authorized_keys
    
    to make sure we haven't added extra keys that you weren't expecting.
    [root@vasmon home]# 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2015-01-03
      • 2016-03-26
      • 2020-06-18
      • 2020-04-24
      • 2011-02-05
      • 2011-02-06
      相关资源
      最近更新 更多