Automate SSH Script
I'm looking for a solution for automate remotely connecting to a linux squid cache server via SSH. Then a script would need to run to stop squid and then restart squid with a different squid.conf file. The entire process needs to be automated because it will need to be ran at around 2am in the morning once or twice a week. Thanks for any help with this problem.
|
This is a job for cron.
Write a script that do what you need and put this script to run by cron at 2am once a week. Let say your script is /usr/local/sbin/myscript. the following line in root's crontab would run myscript at every sunday 2:00am: Code:
#min hour dom month dow command |
The script basically needs to run only the following command:
ssh user@host command where "command" is a program (script) on the remote host to create new squid config, stop squid and restart it. To be able to automatically login using SSH, you should put the public key of your host in ~user/.ssh/authorized_keys file on the remote machine. |
this needs to be setup on demand not at the same time every week. Could be some weeks where it is not ran. The script will be swiching squid to reverse proxy 3 web servers to 3 differnt web servers and this needs to be automated based on web app release schedules.
|
You can use timmeke's suggestion or as I do, comment the line when I do not want it to be run and un-comment it in an on demand base.
Also, you can use the command "at 2:00" to setup one time execution. |
Thanks for the suggestions. I like the "ssh user@host command" method. I just need more info on how to set this up. I don't know how to setup the keys (~user/.ssh/authorized_keys) Ideally we'd want this to be fired off from a Windows machine with no interaction needed.
|
On Windows, I recommend getting PuTty or some other SSH client application, preferably with a command line interface.
The "ssh user@host" command probably only works on Linux, not on Windows. |
Was just about to post a little script that I made last year it will upload a ssh key to a server of your choice. You can then modify it a bit to run a command on the remote hsot via ssh, I did this and it seems to work fine, hopefully remember exacktly what I did.
#!/bin/bash USER="bill" ADDRESS="192.168.1.15" PORT="5678" NEWKEY="yes" keygen () { if [ $NEWKEY == "yes" ]; then ssh-keygen -t dsa -f ~/.ssh/id_dsa fi } checkfile () { if [ -f ~/.ssh/authorized_keys2 ]; then touch ~/.ssh/authorized_keys2 fi } # First let create the directory on the remote host them upload the certificate. sshupload () { cat ~/.ssh/id_dsa.pub | ssh -p $PORT $USER@$ADDRESS 'sh -c "mkdir ~/.ssh && cat - >>~/.ssh/authorized_keys2 && chmod 600 ~/.ssh/authorized_keys2"' } ## Our Main Menu press_enter () { echo "" echo -n "Press Enter to continue" read clear } selection= until [ "$selection" = "0" ]; do echo "" echo "SSH Keygen PROGRAM MENU" echo "1 - Generate & Upload New Key" echo "2 - Upload Old Key" echo "" echo "0 - exit program" echo "" echo -n "Enter selection: " read selection echo "" case $selection in 1 ) checkfile ; keygen ; sshupload ;; 2 ) sshupload ;; 0 ) exit ;; * ) echo "Please enter 1, 2 or 0"; press_enter esac done exit 0 Then all you need to do to execute the command on the emote host, something like this: ssh -p 5678 bill@192.168.1.15 'sh -c "add the command for the remote host here"' |
@fotoguy: the OP mentioned that he'd like to run this script from a Windows box.
Your script probably won't work on Windows. @Gentoo20: As an alternative to PuTty, Cygwin can be used as well, in comination with ssh. See also http://pigtail.net/LRP/printsrv/cygwin-ssh.html |
Ooops didn't see that it was coming from a windoze box
|
I don't have an authorized_keys file or a .ssh directory. Should these already be created or can I manaully create them?
|
The first time you use ssh to connect to another machine it will make them for you, but you can make the .ssh directory yourself it you want. The script I posted will make the directory on the remote host if it doesn't already have one.
|
All times are GMT -5. The time now is 11:31 AM. |