-1

Currently trying to run a bash script on startup to automatically install squid, however the command I'm running requires input.

Currently the script i have is:

#!/bin/sh
PROXY_USER=user1
PROXY_PASS=password1
wget https://raw.githubusercontent.com/hidden-refuge/spi/master/spi && bash spi -rhel7 && rm spi
#After i run this command it asks "Enter username" 
#followed by "Enter password" and "Renter password"
echo $PROXY_USER
echo $PROXY_PASS
echo $PROXY_PASS
echo yes

However i am unable to get the input working, and the script fails to create a username and password. I'm running centos 7.

lomo
  • 11
  • 1

2 Answers2

1

Look you are calling some tools which act in interactive mode, so as dani-gehtdichnixan mentioned at (passing arguments to an interactive program non interactively) you can use expect utilities.

Install expect at debian:

apt-get install expect

Create a script call spi-install.exp which could look like this:

#!/usr/bin/env expect
set user username
set pass your-pass

spawn spi -rhel7 
expect "Enter username"
send "$user\r"

expect "Renter password"
send "$pass\r"

Then call it at your main bash script:

#!/bin/bash 
wget https://raw.githubusercontent.com/hidden-refuge/spi/master/spi  && ./spi-install.exp  && rm spi

Expect is used to automate control of interactive applications such as Telnet, FTP, passwd, fsck, rlogin, tip, SSH, and others. Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Tk, another Tcl extension, can be used to provide a GUI.

https://en.wikipedia.org/wiki/Expect

Reference :

[1] passing arguments to an interactive program non interactively

[2] https://askubuntu.com/questions/307067/how-to-execute-sudo-commands-with-expect-send-commands-in-bash-script

[3] https://superuser.com/questions/488713/what-is-the-meaning-of-spawn-linux-shell-commands-centos6

H.Tibat
  • 353
  • 2
  • 9
0

Try just passing the values to bash's stdin

#!/bin/sh
PROXY_USER=user1
PROXY_PASS=password1
if wget https://raw.githubusercontent.com/hidden-refuge/spi/master/spi; then
    printf "%s\n" "$PROXY_USER" "$PROXY_PASS" "$PROXY_PASS" yes | bash spi -rhel7
    rm spi
fi
glenn jackman
  • 238,783
  • 38
  • 220
  • 352