How can i get a list of running processes on my running EC2 instance uisng SSM? - aws-ssm

I want to get a list of processes running on my EC2 instance(Linux, windows or whatever) using a script.
I researched a bit and got to know I will have to install SSM agent on my instance and then create document containing command to list processes. Document contain "content" field but how do I insert ps -A command in content? It's all too confusing?
I also want to know that is it possible using creating a SSH connection to my instance and log in to instance all in a single script?

Yes, you can get a list of processes running on your EC2 instance. You don't need to create a new document to do that. You're looking for the send-command. The easiest way to figure out how to do it is to use the AWS SSM console to execute the command through the console, and it will give you the command to run on the command line to repeat it.
You can also execute a command through SSH. That command would be:
ssh -i mykey.pem ec2-user#<insert your machine's IP address> "ps -A"
Essentially just enter the command after your regular SSH command.

Related

How to run shell script in another server in a script?

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up
Thanks!
Just pass the command as an argument to ssh.
ssh someserver /path/to/some/script.bsh
Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp. Simply
ssh node2 < /path-of-the-script-including-the-filename
Another way, using expect package.
Disclaimer: This you can use for testing environments since it has an
open password. but depends on your usecase
If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .sh script.
expect -c 'spawn ssh user#10.11.12.13 "/path/to/my.sh"; expect "assword:"; send "Y0urp#ssw0rd\r"; interact'

pull a ssh results to variable

if I ssh to another linux env, and run a command that gives me a result, can I grab that ssh result and store it locally? I have a ksh script that run locally on one linux box right now, but I need to get some parameters from another linux box into that script.
Sure, ssh is a command just like any other, but this will be trickier if you want to ssh interactively and do it, but this could work:
var=$(ssh user#host command_with_output)
then $var will show the output of command_with_output
All this works best if ssh doesn't require a password as well

Run bash script upon ssh

I have a host on which I created a script .
The script is being executed whenever the user is logging in via ssh bashrc launches the script.
Now I'm trying to get the script to execute even if the user is not actually logging in , and just running a command .
For example I want the script to be executed if a user is running the following :
ssh user#host.com some_command
Is there a way to achieve the above?
A solution affecting all the users could be using pam-exec and launch a script on the user login event. Check the pam-exec manual page and an example on how to use it pam-exec scripting.
A simple solution for a single user should be add the script in the rc file of the ssh user, add your script to:
~/.ssh/rc
I've done some tests and the rc solution works fine in your case, it gets executed when the user launches a remote command via ssh.
If you don't have a rc file just create it.
you can edit authorized_keys file and add a COMMAND , something like :
command="/home/michale/bin/dothis.sh" ...public key...
for more details read ssh and authorized_keys documentations.

Replicating SCP command in Shell script in linux?

Hi I have been given a task of copying files from a given server to local machine. Even I can do it manually using the command line but I need to write a script to automate it. I dont have any clue how to do it using shell, how to give the password which we would have done manually. I went through other posts but did not get the precise answer.
Are there better ways than using SCP command?
Thanks in advance
The preferred + more secure way to do this is to set up ssh key pairs
That being said, if there's a specific need to supply passwords as part of your shell script, you can use pscp, which is part of putty-tools:
If you are on ubuntu, you can install it by:
sudo apt-get install putty-tools
(Or use equivalent package managers depending on your system)
Here's an example script of how to use pscp:
#!/bin/bash
password=hello_world
login=root
IP=127.0.0.1
src_dir=/var/log
src_file_name=abc.txt
dest_folder=/home/username/temp/
pscp -scp -pw $password $login#$IP:$src_dir/$src_file_name $dest_folder
This copies /var/log/abc.txt from the specified remote server to your local /home/username/temp/

How to open ssh and launch a command at the same time?

I have an application (Java, but could be anything else) which needs to launch another application. This is not on the same machine.
Manually, I would launch the application in this way:
ssh myself#machine -X
/..../myapplication
I tried to put the two commands in a text file called mycommand and
source mycommand
...but the second command will be executed on the local machine just after having closed the SSH section.
Do you know if there is a way to open an ssh and launch an application from the other machine at the same time without the user intervention?
If after the command you don't need to execute other command in the SSH shell, you can use the following command
ssh myself#machine -X myapplication
The shell will execute the command and then close the ssh connection

Resources