bash 如何将密码作为参数传递给 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32271258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass password as an argument to a shell script
提问by apurv
I am in process of automating installation of sage through ansible-playbook. In that I need to run two shell scripts. Here is how the first shell script look:
我正在通过 ansible-playbook 自动安装 sage。因为我需要运行两个 shell 脚本。这是第一个 shell 脚本的外观:
#!/bin/bash
# Creating Sage notebook
dir="/root/.sage/sage_notebook.sagenb"
screen -S "Sage_Server" sage -c 'notebook(interface="", directory=$dir, port=80, accounts=true)'
This is the second shell script's code:
这是第二个 shell 脚本的代码:
#!/bin/bash
# Creating Sage inotebook
address=$(hostname --ip-address)
sage -c "inotebook(interface=" "'$address'" ",port=80,accounts=true)"
And this is how the playbook looks:
这就是剧本的样子:
---
- hosts: localhost
remote_user: root
tasks:
- name : update system
apt : update_cache=yes
- name : install m4
apt : name=m4 state=present
- name : install build-essential
apt : name=build-essential state=present
- name : install gcc
apt : name=gcc state=present
- name : install gfortran
apt : name=gfortran state=present
- name : install libssl-dev
apt : name=libssl-dev state=present
- name : install python-software-properties
apt : name=python-software-properties state=present
- name : add sage ppa repo
apt_repository: repo='ppa:aims/sagemath'
- name : update system
apt : update_cache=yes
- name : install dvipng
apt : name=dvipng state=present
- name : install sage binary
apt : name=sagemath-upstream-binary state=present
- name : invoke create_sagenb script
command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/create_sagenb -i -y
- name : invoke start_sage script
command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/start_sage -i -y
Now when I run the first script, it asks for a new sage password which could be anything. But I am not able to pass that password from the playbook. Still, if I do
现在,当我运行第一个脚本时,它会要求输入一个新的 sage 密码,该密码可以是任何内容。但是我无法从剧本中传递该密码。不过,如果我这样做
ps -ef | grep sh
I could see that the scripts are running but the sage service is not running. It needs the password in order to start the service.
我可以看到脚本正在运行,但 sage 服务没有运行。它需要密码才能启动服务。
Could anyone please tell me how can I provide password as an argument to the shell scripts through command.
谁能告诉我如何通过命令提供密码作为 shell 脚本的参数。
回答by Alepac
回答by Filipe
As a rule of thumb you shouldn't run scripts that needs user input in ansible playbooks.
根据经验,您不应该在 ansible playbook 中运行需要用户输入的脚本。
You could try to use something like
你可以尝试使用类似的东西
echo "password" | script.sh
or
或者
Create a sage-password
file in /etc
containing the password and:
创建一个包含密码的sage-password
文件,/etc
然后:
script.sh < /etc/sage-password
But this will only work if it is reading from stdin - most applications read password directly from terminal device driver (i.e. /dev/ttyS
# ), in that case this trick won't work.
但这只有在从 stdin 读取时才有效——大多数应用程序直接从终端设备驱动程序(即/dev/ttyS
# )读取密码,在这种情况下,这个技巧将不起作用。
If that's the case, take a look to the sage docs, they should have a more robust way for non-interactive startup.
如果是这种情况,请查看 sage 文档,他们应该有一种更强大的非交互式启动方式。