【问题标题】:scp won't work from bash script in vagrant boxscp 不能在 vagrant box 中使用 bash 脚本
【发布时间】:2014-08-13 20:06:48
【问题描述】:

我正在使用 vagrant 1.6.3 版本和 vagrant 的默认 linux 框。

我的盒子运行良好,所以在 vagrantfile 中我添加了运行外部 shell 脚本。该脚本包含此命令。

custom.sh 文件

#!/bin/bash

DEPLOY_PASSWORD="xxxxx" &&
DB_NAME="test-sync.sql.gz" &&
apt-get install sshpass &&
sshpass -p "$DEPLOY_PASSWORD" scp -P 22 -v user@test01.admin.com:"~/mysql_dumps/$DB_NAME" "./"

我尝试手动运行该命令,它可以正常运行,没有任何问题,但不能从 bash 脚本运行。这是我通过运行 vagrant provision 得到的错误日志:

 The following SSH command responded with a non-zero exit status.
 Vagrant assumes that this means the command failed!
 chmod +x /tmp/vagrant-shell && /tmp/vagrant-shell test-sync.sql.gz

流浪文件

    Vagrant.configure("2") do |config|

  # Load config JSON.
  config_json = JSON.parse(File.read("config.json"))

  # Prepare base box.
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  # Configure networking.
  config.vm.network :private_network, ip: config_json["vm"]["ip"]

  # Configure forwarded ports.
  config.vm.network "forwarded_port", guest: 35729, host: 35729, protocol: "tcp", auto_correct: true
  config.vm.network "forwarded_port", guest: 8983, host: 8983, protocol: "tcp", auto_correct: true
  # User defined forwarded ports.
  config_json["vm"]["forwarded_ports"].each do |port|
    config.vm.network "forwarded_port", guest: port["guest_port"],
      host: port["host_port"], protocol: port["protocol"], auto_correct: true
  end

  # Customize provider.
  config.vm.provider :virtualbox do |vb|
    # RAM.
    vb.customize ["modifyvm", :id, "--memory", config_json["vm"]["memory"]]

    # Synced Folders.
    config_json["vm"]["synced_folders"].each do |folder|
      case folder["type"]
      when "nfs"
        config.vm.synced_folder folder["host_path"], folder["guest_path"], type: "nfs"
        # This uses uid and gid of the user that started vagrant.
        config.nfs.map_uid = Process.uid
        config.nfs.map_gid = Process.gid
      else
        config.vm.synced_folder folder["host_path"], folder["guest_path"]
      end
    end
  end

  # Run initial shell script.
  config.vm.provision :shell, :path => "chef/shell/initial.sh"

  # Customize provisioner.
  config.vm.provision :chef_solo do |chef|
    chef.json = config_json
    chef.custom_config_path = "chef/solo.rb"
    chef.cookbooks_path = ["chef/cookbooks/berks", "chef/cookbooks/core", "chef/cookbooks/custom"]
    chef.data_bags_path = "chef/data_bags"
    chef.roles_path = "chef/roles"
    chef.add_role "vdd"
  end

  # Run final shell script.
  config.vm.provision :shell, :path => "chef/shell/final.sh", :args => config_json["vm"]["ip"]
  #custom tasks
  config.vm.provision :shell, :path => "custom.sh", :args => config_json["test_config"]["db_name"]
end

【问题讨论】:

  • 为了解决这个问题,我将从一个简单的脚本开始,它只执行类似echo Hello 的操作。然后,您可以确定是 Vagrant 还是您的脚本有问题。您能否也发布您的整个 Vagrantfile 以显示您是如何运行脚本的?
  • 我尝试了 echo Hello,它按预期工作,只有当它执行任何类型的 ssh 时,脚本才会在 vagrant 中失败。

标签: linux bash vagrant


【解决方案1】:

我不使用 sshpass(在脚本中明文设置密码的想法让我感到畏缩),但手册页似乎建议在脚本中设置变量 SSHPASS 而不是使用-p 咒语...

EXAMPLES
   Run rsync over SSH using password authentication, passing the password on the command line:
   rsync --rsh='sshpass -p 12345 ssh -l test' host.example.com:path .
   To do the same from a bourne shell script in a marginally less exposed way:
   SSHPASS=12345 rsync --rsh='sshpass -e ssh -l test' host.example.com:path .

【讨论】:

  • 再说一次,我的陈述没有任何问题,按预期工作正常。它只是不会在流浪者中工作。请检查我的 ?再次。
  • “我尝试手动运行该命令,它运行良好,没有任何问题,但它在 bash 脚本中不起作用。” ...看来我的回应是试图解决这个问题? /我耸了耸肩
  • 对不起,您的方法仍然存在同样的错误。我想告诉你的是,vagrant 中可能有一些设置阻止脚本运行 ssh 命令而不是代码本身。
【解决方案2】:

sshpass 仅在提示以assword: 结尾时有效。在您的情况下,它将是 password for user@test01.admin.com,因为您指定了用户名。

http://sourceforge.net/p/sshpass/code/HEAD/tree/trunk/main.c第362行

你为什么不改用public key autentication

【讨论】:

  • 命令没有问题。当我将它复制并粘贴到终端并正常运行时,它工作正常。
猜你喜欢
  • 2012-11-09
  • 2021-03-13
  • 2017-11-25
  • 1970-01-01
  • 2010-12-30
  • 2016-10-01
  • 2023-03-04
  • 2013-08-06
  • 2016-04-04
相关资源
最近更新 更多