Solution1:use sshpass
#~/bin/myssh.sh
sshpass -p a1234b ssh myname@somehost
You can install by
# Ubuntu/Debian
$ sudo apt-get install sshpass
# Red Hat/Fedora/CentOS
$ sudo yum install sshpass
# Arch Linux
$ sudo pacman -S sshpass
#OS X
brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
or download the Source Code from here, then
tar xvzf sshpass-1.08.tar.gz
cd sshpass-1.08.tar.gz
./configure
sudo make install
Solution2:Set SSH passwordless login
Let's say you need to SSH into [email protected](Remote server B)
with the password 2b2b2b
from [email protected](Client server A)
.
Generate the public key(.ssh/id_rsa.pub)
and private key(.ssh/id_rsa)
in A with the following commands
ssh-keygen -t rsa
[Press enter key]
[Press enter key]
[Press enter key]
Use the following command to distribute the generated public key(.ssh/id_rsa.pub)
to server B under bbb‘s .ssh directory as a file name authorized_keys
ssh-copy-id [email protected]
You need to enter a password for the first ssh login, and it will be logged in automatically in the future, no need to enter it again!
ssh [email protected] [Enter]
2b2b2b
And then your script can be
#~/bin/myssh.sh
ssh myname@somehost
ps -aux
, you shouldn't normally run commands by typing your password because other users on the same computer may be able to see the password by runningps -aux
. if practical, you also want to use public key authentication instead, as mentioned in the other answer. this allows you to separate authentication info from your script so you can share your script with others worry-free, and later decide to enable encryption on your ~/.ssh folder without also encrypting your script. – Gollin