Sometimes I find myself needing to run a few command on a remote machine. I’ll
usually ssh into the box and hack away. But, there is an a alternative. You can
run command over SSH!
In the example below, I wasnted to keep the configuration files in the git
repository, but wanted a quick and dirty way to deploy the changes.
The first half of the script copies files, using scp, from the local machine to
the remote server.
The second half of the script (starting on line 12) runs commands from the
remote machine. The files are moved to the correct locations and nginx and
haproxy are restarted.
1234567891011121314151617181920
#!/bin/bashPEM=id_rsa.pub
HOST=ec2-54-234-130-49.compute-1.amazonaws.com
scp -i ~/.ssh/$PEM ./surrogate_pop.conf ubuntu@$HOST:/tmp
scp -i ~/.ssh/$PEM ./haproxy.cfg ubuntu@$HOST:/tmp
scp -i ~/.ssh/$PEM ./traffic_cop.lua ubuntu@$HOST:/tmp
scp -i ~/.ssh/$PEM ./allowed_domains.lua ubuntu@$HOST:/tmp
## These are executed on the remote hostssh -i ~/.ssh/$PEM ubuntu@$HOST'bash -s'<<EOFsudo mv /tmp/traffic_cop.lua /usr/share/nginx/traffic_cop.luasudo mv /tmp/allowed_domains.lua /usr/share/nginx/allowed_domains.luasudo mv /tmp/surrogate_pop.conf /etc/nginx/sites-enabled/surrogate_pop.confsudo service nginx restartsudo mv /tmp/haproxy.cfg /etc/haproxy/haproxy.cfgsudo service haproxy restartEOF