admin管理员组

文章数量:1431406

I have an SSM environment in AWS, and I have client machines that are joining without any issues. I am able to remote connect to the terminal and run commands, everything works up to this point.

Now I am trying to run terminal commands on the devices en masse. So I would like to, for example, run this:

echo 192.168.1.1 MyServer.domain >> /etc/hosts
apt install realmd
echo MyPassword | realm join -v --user=MyUsername domain
sudo reboot

So to package this script up in a tidy little unit of some kind that I can push out to devices as I require. But I can't seem to find any way that this can be done.

I have an SSM environment in AWS, and I have client machines that are joining without any issues. I am able to remote connect to the terminal and run commands, everything works up to this point.

Now I am trying to run terminal commands on the devices en masse. So I would like to, for example, run this:

echo 192.168.1.1 MyServer.domain >> /etc/hosts
apt install realmd
echo MyPassword | realm join -v --user=MyUsername domain
sudo reboot

So to package this script up in a tidy little unit of some kind that I can push out to devices as I require. But I can't seem to find any way that this can be done.

Share Improve this question edited Nov 19, 2024 at 7:36 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Nov 19, 2024 at 6:39 BisclavretBisclavret 1,35110 gold badges40 silver badges71 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

maybe I'm misunderstanding the question, but SSM has a concept of "Documents" where you can store your scripts and supports a "Run Command" which can be used to run the document against your "fleet" of machines.

It even supports rate controls and more advanced feature.

Link for the documentation can be found here: https://docs.aws.amazon/systems-manager/latest/userguide/send-commands-multiple.html

you can try this script :

#!/bin/bash

instances=(i-instance-1 i-instance-2)
for instance in "${instances[@]}"; do
    aws ssm send-command --document-name "AWS-RunShellScript"\
                         --targets "Key=instanceIds,Values=$instance"\
                         --parameters 'commands=["echo 192.168.1.1 MyServer.domain >> /etc/hosts","apt install realmd","echo MyPassword | realm join -v --user=MyUsername domain","reboot"]'
done

Try the following:

aws ssm send-command \
--document-name "AWS-RunShellScript" \
--targets '[{"Key":"InstanceIds","Values":["instance-id"]}]' \
--parameters '{"commands":["#!/bin/bash","echo 192.168.1.1 MyServer.domain >> /etc/hosts","apt install realmd","echo MyPassword | realm join -v --user=MyUsername domain","sudo reboot"]}'

For your instances, use an array with your instance ids in it. You would run this from your machine that has the aws cli on it and it would connect to all your remote instances. Each line of your shell script would be a separate command in quotation marks.

Here is the reference I found on the AWS documentation for future reference: https://docs.aws.amazon/systems-manager/latest/userguide/walkthrough-cli.html#walkthrough-cli-example-3

本文标签: linuxUsing SSM to push terminal commandsStack Overflow