LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Automate SSH Script (https://www.linuxquestions.org/questions/linux-general-1/automate-ssh-script-426594/)

Gentoo20 03-20-2006 09:28 AM

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.

marozsas 03-20-2006 09:48 AM

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
#0-59  0-23    1-31    1-12    0-7
#                              0, 7 is Sunday. Names are ok.
0      2      *      *      Sun    /usr/local/sbin/myscript

cheers,

timmeke 03-20-2006 10:28 AM

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.

Gentoo20 03-20-2006 10:34 AM

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.

marozsas 03-20-2006 10:54 AM

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.

Gentoo20 03-20-2006 03:18 PM

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.

timmeke 03-21-2006 02:47 AM

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.

fotoguy 03-21-2006 03:28 AM

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"'

timmeke 03-21-2006 05:21 AM

@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

fotoguy 03-21-2006 11:03 PM

Ooops didn't see that it was coming from a windoze box

Gentoo20 03-22-2006 03:19 PM

I don't have an authorized_keys file or a .ssh directory. Should these already be created or can I manaully create them?

fotoguy 03-22-2006 06:21 PM

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.