如何将密码作为参数传递给Shell脚本

5

我正在通过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)'

这是第二个shell脚本的代码:
#!/bin/bash
# Creating Sage inotebook

address=$(hostname --ip-address)
sage -c "inotebook(interface=" "'$address'" ",port=80,accounts=true)"

以下是playbook的样式:

---
- 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

现在当我运行第一个脚本时,它会要求输入一个新的sage密码,这个密码可以是任何东西。但是我无法从playbook中传递该密码。 即使如此,如果我这样做
ps -ef | grep sh

我看到脚本在运行,但是Sage服务没有启动。它需要密码才能启动服务。

请问有人可以告诉我如何通过命令将密码作为参数传递给shell脚本吗?


4
将密码传递给shell脚本的技术与开枪打自己的脚所用的技术相同,需要避免使用。没有不引入安全漏洞的方法。 - William Pursell
2个回答

2
我不知道什么是“sage”,也不知道如何以其他方式提供密码,但如果程序要求输入密码,那么你可能可以像这里建议的那样使用expect

剧本也在抛出错误并要求运行 dpkg --configure -a。 - apurv

2

作为一条经验法则,你不应该在ansible playbooks中运行需要用户输入的脚本。

你可以尝试使用类似以下命令:

echo "password" | script.sh

或者

/etc目录下创建一个名为sage-password的文件,其中包含密码:

script.sh < /etc/sage-password

但是,这只适用于从标准输入读取密码的情况 - 大多数应用程序直接从终端设备驱动程序(即/dev/ttyS#)中读取密码,在这种情况下,这个技巧就不起作用了。
如果是这种情况,请查看sage文档,他们应该有一种更健壮的非交互式启动方式。

回显命令可能会根据脚本的实现而工作或不工作。例如,如果它启动一个 tty 并在那里要求密码,则不起作用。expect 及其变体应该是更好、更可靠的选择。 - Jason Hu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接