When executing expect
scripts, arguments are visible on ps ax
which can be a security vulnerability if they are sensitive.
Trying to automate opening a tab on iTerm2, running ssh admin@host
and entering the passphrase when asked Enter passphrase for key '/Users/admin/.ssh/key'
(the key is encrypted using that passphrase).
Host host
HostName 1.2.3.4
IdentityFile ~/.ssh/key
I would like to supply the passphrase to bash using read -sp 'Passphrase: ' passphrase
and then pipe it to expect
(which isn’t perfect at all from an OPSEC perspective but much better than having the passphrase leaked on ps ax
).
Perhaps there is a better way?
Bellow is some code that works but leaks the passphrase on ps ax
. Commented out is what I wish was possible (piping the passphrase to expect
).
batch.sh
#!/bin/bash
function new_tab() {
command=${1//\"/\\\"}
osascript \
-e "tell application \"iTerm2\"" \
-e "tell current window" \
-e "create tab with default profile" \
-e "delay 1" \
-e "tell current session" \
-e "write text \"$command\"" \
-e "end tell" \
-e "end tell" \
-e "end tell" > /dev/null
}
hostnames=(
"hostname-1"
"hostname-2"
)
read -sp 'Passphrase: ' passphrase
for hostname in "${hostnames[@]}"; do
# new_tab "echo $passphrase | expect $(pwd)/expect.exp \"$hostname\""
new_tab "expect $(pwd)/expect.exp \"$hostname\" \"$passphrase\""
done
expect.exp
#!/usr/bin/expect
set hostname [lindex $argv 0]
set passphrase [lindex $argv 1]
spawn ssh admin@$hostname
expect "passphrase"
send "$passphrase\r"
interact