write a shell script to ssh to a remote machine and execute commands

后端 未结 9 1380
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 19:11

I have two questions:

  1. There are multiple remote linux machines, and I need to write a shell script which will execute the same set of commands in each machine.
9条回答
  •  爱一瞬间的悲伤
    2020-11-28 19:39

    This work for me.

    Syntax : ssh -i pemfile.pem user_name@ip_address 'command_1 ; command 2; command 3'

    #! /bin/bash
    
    echo "########### connecting to server and run commands in sequence ###########"
    ssh -i ~/.ssh/ec2_instance.pem ubuntu@ip_address 'touch a.txt; touch b.txt; sudo systemctl status tomcat.service'
    

提交回复
热议问题