簡體   English   中英

如何將密碼作為參數傳遞給shell腳本

[英]How to pass password as an argument to a shell script

我正在通過ansible-playbook自動安裝鼠尾草。 在那里我需要運行兩個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

現在,當我運行第一個腳本時,它會要求輸入一個新的sage密碼。 但是我無法從劇本中傳遞密碼。 如果我這樣做的話

ps -ef | grep sh

我可以看到腳本正在運行但是sage服務沒有運行。 它需要密碼才能啟動服務。

有誰能告訴我如何通過命令提供密碼作為shell腳本的參數。

根據經驗,您不應該在ansible playbooks中運行需要用戶輸入的腳本。

你可以試着用類似的東西

echo "password" | script.sh

要么

/etc中創建一個包含密碼的sage-password文件,並且:

script.sh < /etc/sage-password

但這只有在從stdin讀取時才會起作用 - 大多數應用程序直接從終端設備驅動程序讀取密碼(即/dev/ttyS #),在這種情況下,這個技巧將無效。

如果是這種情況,請查看sage文檔,他們應該有一個更強大的非交互式啟動方式。

我不知道sage ,也不知道如何以另一種方式提供密碼,但是如果程序要求輸入密碼,那么你可能expect按照這里的建議使用密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM