I'm trying to make a bash script used to unlock an external drive which is password-protected by WD proprietary software.
Since I'm combining this with other bash scripts, I need it to take the password provided at the beginning with read -s -p "Enter WD password: " wdpass and use it when prompted by the WD script.
This is what I have so far:
#!/bin/bash
sudo blkid #only need this to get the sudo password right away
wait
read -p "Enter drive ID (ie. sda): " driveid
read -s -p "Enter WD password: " wdpass
sudo apt install python3 python3-dev python3-pip git; sudo pip3 install git+
sudo pip3 install --user git+
cd ~; wget
sudo ~/wdpassport-utils.py --unlock --device /dev/$driveid
expect "[wdpassport] password for /dev/${driveid}: "
send "$wdpass"
waitI also tried the following combinations instead of expect and send (but nothing worked):
sudo ~/wdpassport-utils.py --unlock --device /dev/$driveid <(echo "$wdpass")echo "$wdpass"echo $wdpass"$wdpass"$wdpass
2 Answers
The wdpassport-utils.py script you are referring to uses Python getpass.getpass() function to get the password. Maybe the simplest method would be to just change this with your bash script (eg. using sed) to input() (which will read input from stdin) before calling wdpassport-utils.py, and then use the line mentioned above:
sudo ~/wdpassport-utils.py --unlock --device /dev/$driveid <(echo "$wdpass")or, in more common form:
echo "$wdpass" | sudo ~/wdpassport-utils.py --unlock --device /dev/$driveid 3 You should use expect extension to Tcl. It is intended for such automation tasks and more. There is a whole book (Exploring Expect) written about it and a rich wikipedia page
In my comment, I stressed the important difference between an expect and a bash script. In this case, it should be expect script, here is an example:
#!/usr/bin/expect -f
spawn ssh aspen
expect "password: "
send "PASSWORD\r"
expect "$ "
send "ps -ef |grep apache\r"
expect "$ "
send "exit\r" You will have to read some guides, for example Automating with Expect Scripts from admin-magazine.com.
0