Run SSH command in .yml file - amazon-ec2

I am developing gitlab ci for test automation and deploy automation.
Test automation is complete. Now I want to complete deploy automation.
I can login to EC2 machine using ssh -i test.pem 123.345.123.113 from terminal.
How can put ssh -i test.pem 123.345.123.113 in .yml file?

You can directly run a command through ssh like that:
ssh user1#server 'command'

Related

How do I execute a command with an automated script on a remote machine in a golang CLI?

I'm researching ssh and the command to follow. How would I go about executing a shell command in an ssh server automatically via script with golang CLI? I need to write a golang CLI automated script that can SSH into a remote machine via this format: "ip username:password". For example I have a file named "logins.txt" lets say 400-500 ssh logins. I would like the script to execute a shell command while logging into 400-500 ssh servers automatically.

SCP fails almost immediately with 'lost connection' after successful ssh connection

I am writing a script to upload DLLs to a remote machine. I first use a command to ssh in and stop the service running. I then try to upload the new DLLs via scp, but it fails basically immediately with lost connection.
My entire shell script looks like this:
ssh -p 29170 DLL_Uploader#XXX.XXX.XX.XXX "powershell.exe; .\stop_file_watcher.ps1; exit";
scp -P 29170 $1 "DLL_Uploader#XXX.XXX.XX.XXX:/Bin/File Watcher Service"
ssh -p 29170 DLL_Uploader#XXX.XXX.XX.XXX "powershell.exe; .\start_file_watcher.ps1; exit";
You can see a pastebin of this scp debug output with -vvv here.

Jenkins Execute shell is failed after executing ssh to a remote server

I am creating a Jenkins job in which am running a ssh command to execute a script for comparing two folders using diff command on a remote server. Script is running fine, output file is getting created. But after this command Jenkins execute shell block is failed.
Command:
ssh -T user#dtest.com "bash /tmp/sample.sh" >> result.txt
Log:
ssh -T user#dtest.com "bash /tmp/sample.sh" >> result.txt
stdin: is not a tty
"Execute shell" is marked as failure
I am not sure what sample.sh is supposed to do, but I understand that you are trying to capture what is logged by this script.
I would try several solutions:
ssh -T user#dtest.com "bash /tmp/sample.sh >> result.txt"
This should save your output in your remote server. Then you could copy this file from remote to local using:
scp user#dtest.com:/remote/dir/result.txt /local/dir/
More context: Copying files from server to local computer using ssh
If you are choosing this solution, you could also consider to write your result.txt directly from your script, and keep the console output for important logging purpose.
Another Solution I could think of would be to use
ssh user#dtest.com "bash /tmp/sample.sh" > result.txt
With this solution you will redirect your output directly to your local machine.
But you will need to delete the ssh "-T" option. And you will run into other problems with Jenkins. So this might not fit you.
ssh -T Disables pseudo-tty allocation, what sounds like your problem's root cause. (https://docs.oracle.com/cd/E36784_01/html/E36870/ssh-1.html)

rsync to move file from local to remote machine and execute command on remote machine

I have one file in my local Linux machine and I want to move that one to remote machine and then execute one command on remote machine to restart the service. The issue is after moving the file the remote connection get closed. I have used the following command:
rsync --remove-source-files -av -e ssh -t /home/testdata.txt root#vdstvmaster:/home/; service restart
If I execute the above command file is successfully moved to remote machine. But the second command (service restart) is not executed on remote machine.
rsync can use a remote shell to perform the copy operation. But it is not meant as a "general-purpose" remote shell application. Just invoke the second command over SSH locally after the rsync command like this:
rsync --remove-source-files -av -e ssh -t /home/testdata.txt root#vdstvmaster:/home/
ssh root#vdstvmaster service restart
BTW some people may consider remotely logging into another machine as root bad security.

Help with ec2-api-tools for Ubuntu

I'm following this tutorial: https://help.ubuntu.com/community/EC2StartersGuide
To start an instance, you run:
ec2-run-instances ami-xxxxx -k ec2-keypair
Then run:
ec2-describe-instances
which gets you the external host name of the instance.
And later, to ssh, you run:
ssh -i /path/to/ec2-keypair.pem ubuntu#<external-host-name>
This works fine, but here is my question:
How can I automate this in a bash script? Can I somehow parse the response returned from "ec2-describe-instances"?
I don't know what the output of ec2-describe-instances looks like, but if it's simply the hostname, then you should be able to do:
host=$(ec2-describe-instances)
ssh -i /path/to/ec2-keypair.pem ubuntu#$host

Resources