LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-20-2006, 09:28 AM   #1
Gentoo20
LQ Newbie
 
Registered: Oct 2004
Posts: 10

Rep: Reputation: 0
Question 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.
 
Old 03-20-2006, 09:48 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,465
Blog Entries: 1

Rep: Reputation: 66
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,
 
Old 03-20-2006, 10:28 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 03-20-2006, 10:34 AM   #4
Gentoo20
LQ Newbie
 
Registered: Oct 2004
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 03-20-2006, 10:54 AM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,465
Blog Entries: 1

Rep: Reputation: 66
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.
 
Old 03-20-2006, 03:18 PM   #6
Gentoo20
LQ Newbie
 
Registered: Oct 2004
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 03-21-2006, 02:47 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 03-21-2006, 03:28 AM   #8
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,290

Rep: Reputation: 62
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"'
 
Old 03-21-2006, 05:21 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
@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
 
Old 03-21-2006, 11:03 PM   #10
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,290

Rep: Reputation: 62
Ooops didn't see that it was coming from a windoze box
 
Old 03-22-2006, 03:19 PM   #11
Gentoo20
LQ Newbie
 
Registered: Oct 2004
Posts: 10

Original Poster
Rep: Reputation: 0
I don't have an authorized_keys file or a .ssh directory. Should these already be created or can I manaully create them?
 
Old 03-22-2006, 06:21 PM   #12
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,290

Rep: Reputation: 62
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to automate ssh login? Lotharster Linux - Networking 2 12-05-2005 12:54 PM
how to automate telnet login, script or something kiwibird Linux - Networking 2 11-09-2005 02:02 PM
automate script w/ cron viniosity Linux - Newbie 5 02-02-2005 03:13 PM
Kermit Script to Automate FTP SSL/TLS fiddelm3742 Linux - Software 0 05-18-2004 11:53 PM
Script that would automate adding users zyft02 Linux - Newbie 4 02-25-2002 11:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration