2015-08-28 432 views
3

我正在通过ansible-playbook自动安装圣人。因为我需要运行两个shell脚本。 下面是第一个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)" 

这是剧本的样子:

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

现在,当我运行第一个脚本,它会要求一个新的可能是任何东西的圣人密码。但是我无法从剧本中传递密码。 不过,如果我做

ps -ef | grep sh 

我可以看到脚本运行,但圣人服务没有运行。 它需要密码才能启动服务。

任何人都可以请告诉我如何通过命令将密码作为参数提供给shell脚本。

+3

用于将密码传递给shell脚本的技术与用于在脚中拍摄自己的技巧相同。你避免它。没有引入安全漏洞就没有办法做到这一点。 –

回答

2

我不知道sage,不知道如何以另一种方式提供一个密码,但如果程序要求输入密码比你也许可以使用expect的建议here

+0

该剧本也抛出错误,并要求运行 根据脚本的实施,dpkg --configure -a – apurv

3

作为一条经验法则,您不应该在完整的剧本中运行需要用户输入的脚本。

你可以尝试使用类似

echo "password" | script.sh 

创建sage-password文件/etc包含密码和:

script.sh < /etc/sage-password 

但是,这如果是读只会工作从标准输入 - 大多数应用程序直接从终端设备驱动程序(即/dev/ttyS#)读取密码,在这种情况下,这三CK不会工作。

如果是这样,请看看sage文档,他们应该有一个非交互式启动更健壮的方式。

+0

回声可能会或可能不起作用。例如,如果它启动一个tty并在那里要求密码,它将不起作用。 “预期”及其差异应该是更好,更有保证的选择。 – HuStmpHrrr