Password omission expect Automatic SSH

Automating SSH with expect for Password-Protected Servers

In Linux PC/server command line and Bash language, I'll introduce a method to automatically SSH through expect to servers where password omission is not possible. For VPS and your own servers it's fine, but in the case of Xserver, SSH password omission is somehow not possible. I managed to do it with expect, so I'm introducing it.

Shou Arisaka
1 min read
Nov 26, 2025

In Linux PC/server command line and Bash language, I’ll introduce a method to automatically SSH through expect to servers where password omission is not possible.

For VPS and your own servers it’s fine, but in the case of Xserver, SSH password omission is somehow not possible.

I managed to do it with expect, so I’m introducing it.


expecto(){

: usage: expecto [command] [password]
# e.g. expecto 'ssh -R 52698:localhost:52698 -p 10022 -i ~/.ssh/example.key [email protected]' "${PASSWORD}"

cat << EOT > ~/tmp/tmp && chmod 755 ~/tmp/tmp && ~/tmp/tmp && > ~/tmp/tmp
#!/usr/bin/expect -f
# exp_internal 1    ; # uncomment to turn on expect debugging
set timeout -1
spawn ${1}
expect {
    "passphrase" {
      send "${2}\n"
      interact
      exit 0
    }
    "password" {
      send "${2}\n"
      interact
      exit 0
    }
  }
EOT

}

When writing SSH command directly

expecto 'ssh -R 52698:localhost:52698 -p 10022 -i ~/.ssh/yuis.key [email protected]' $XSERV_PASSWORD

When using aliased ssh command, it needs to be expanded

alias xserv='ssh -R 52698:localhost:52698 -p 10022 -i ~/.ssh/yuis.key [email protected]'
expecto \"$(echo ${BASH_ALIASES[xserv]})\" $XSERV_PASSWORD

When using custom-defined Bash commands

expecto 'bash -ic "sharefile hoge.md"' $XSERV_PASSWORD

Share this article

Shou Arisaka Nov 26, 2025

🔗 Copy Links