I have 6 servers in my testing environment. I want to write a script to stop the Tomcat server in all boxes by logging into one box instead of logging into each and every box.

Please help me with shell scripting, SSH and executing a script in all other boxes. I use SSH with a password and custom port number.

New contributor
nithi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You should combine two related Ask Ubuntu answers, one for logging into SSH via specific port and one for running commands on remote server , into one script. Of course, you'd need to have some form of data-structure to hold server+port correspondence. That could be either bash associative arrays or with POSIX shell script - a case statement. Assuming your username on all hosts is the same, we could come up with something like this:

# bar host has ssh server on port 22, while baz - on port 2222
    case "$1" in
        "bar") echo 22 ;;
        "baz") echo 2222 ;;

# set positional parameters of script to hosts and iterate over them
set -- bar baz
for host ; do
    # command substitution will call function, 
    # function's return value will be the port number
    ssh ssh://username@"$host":"$(host_port "$host")" -t "systemctl stop tomcat"

Now, a giant disclaimer: This is just an example. I don't have 6 servers to test the script, so adapt it to your needs as necessary. If you have different usernames on each host, use another case statement

Side note on logging in: if you have private key, which is accepted by all 6 servers it will be easy to log-in just by adding -i ~/.ssh/id_rsa. Alternative to that would be to create a ~/.ssh/config file with key/password information for each host. This is described in Lekensteyn's answer. If you're not familiar with SSH keys (and I understand it's kinda confusing topic), you might wanna read ssh.com article on the topic.


You can use sshpass:

sudo apt install sshpass -y 

then you can execute command on remote server, for example

    # login info
    # command
    sshpass -p $PASSWORD ssh -p $PORT $USER@$HOST touch 1.txt 
  • I have installed sshpass and white I run this script this is showing me bash: ./bscript: Permission denied Error – nithi Feb 12 at 9:20
  • 1
    chmod u+x the script ? – Goufalite Feb 12 at 9:38
  • but where should I mention my custom port number?? we have changed changed our portnumber from 22 to 2222 in all our servers – nithi Feb 12 at 10:08
  • 1
    sshpass -p 'password' ssh -p 2222 user@host touch 1.txt – Yaroslav Urshulyak Feb 12 at 15:29
  • Permission denied, please try again. even after chmod u+x the script.......should I pass the password as root – nithi Feb 15 at 11:08

Your Answer

nithi is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.