I'm new to bash scripting. I want to call a function from my script which will ssh into a remote computer (on my LAN) and run a command.
So far I have:
function run_ssh_command {
target_ip=$1
username=$2
password=$3
cmd=$4
ssh -l ${username} ${target_ip} ${password}
}
I invoke the function from the terminal as follows:
(Note: I give script_name, username and password "real" values when executing the function on my machine. 'ifconfig' is the command I want to run on the remote machine.)
source script_name.sh; run_ssh_command 192.168.X.Y username password ifconfig
Result: Running the above command will get me to the password prompt part of the login process (e.g. same as ssh [email protected])
Question: I want to handle the password entry automatically using the script. What is the "best" way to go about this, in general?
sshpassis an option for you. Usage issshpass -p <PASSWORD> ssh user@host; in your casesshpass -p <PASSWORD> ssh user@host ifconfig.