Approach to writting a script

Hello all,
I've just joined. I did a google search and your site came up,
I had a look and thought I'd like to become a member.

I'm from Ireland.

I've written a few scripts before, but this new task has me foxed.

I would like to figure out the best approach to achieving the following

A single script that does the following
login to machine a (I typically use putty to do this)
login to machine b (as above)
on machine_a run a command such as "service xxx stop; service xxx start"
on machine_a ps -ef | grep AppName to confirm the app started right
on machine_a run top, and grep for a process by name, and batch the cpu% usage result to a file ( I can do this already)

on machine_b cd to a specific directory
on machine_b run a script that points at machine_a

on machine_b, when the script finishes
machine_a will parse the result file, and confirm the average of the cpu% is less than a threshold

That's pretty much it.
The hard part I suppose would be the single script spawing scripts on two different machines and synchonising them?

Thanks for your help in advance! :slight_smile:
Bloke

The easiest approach from a scripting standpoint would be to use ssh to launch commands on the remote system. ssh user@host "command [options]"

Thanks for reply.

When I tried that
ssh root@XXX.XXX.XXX.XXX "ls -al"

it asked me for a password.

As I would like to automate the process by scripting, without having to type in passwords.

Are there alternative approaches.

Set up public key authentication for your account on the server. That should enable password-less login

Setting up public key authentication looks quite complex, is there a less complex way?

I don't know how to do it.

Or an alternative way to issue commands to two boxes from one script!

Public key authentication is quite simple actually.

  1. $ ssh-keygen -b 2048 # This generates your key-pair
  2. SSH to the host, create a directory ~/.ssh with permissions 0700
  3. Paste the contents of (local) ~/.ssh/id_rsa.pub into (remote) ~/.ssh/authorized_keys
  4. Logout and try again to log in. If it doesn't ask for a password, you're all set



# ssh-keygen -b 2048 dsa # This generates your key-pair
# Enter to save to /home/sroot/.ssh/id_dsa
# Enter passphrase (empty for no passphrase): Enter
# Saved in /home/sroot/.ssh/id_dsa.pub
# ssh remoteHost
# mkdir ~/.ssh
# chmod 0700 ~/.ssh
# Paste the contents of (local) ~/.ssh/id_rsa.pub into (remote) ~/.ssh/authorized_keys

Logout and try again to log in. If it doesn't ask for a password, you're all set

I couldn't ssh to the remote machine, so I used a putty I had open already
It wouldn't let me in without a password.
Maybe it's because on the remote it's root, and the local is sroot?

I tried both dsa and rsa

Why couldn't you ssh to the remote machine (and which one of the two)?

I suspect part of the ssh login problem is that on one machine it's username root, on the other it's sroot
Could that be the problem?

Should not be the problem, as long as the target machine has the authorized_keys correctly corresponding with the .pub on the local one. Just make sure that though you are sroot here, you are ssh'ing to root there.

ssh root@dest

Easy on Windows, but how do I do it on rhel4?

Is this a new question for the group?

How odd is that! I thought I started a new thread.
I've been posting to the internet for 15 years, never did that before!

Long time since I visited this thread.

I found this link which describes what I wish to do, BUT
http://www.cyberciti.biz/tips/ssh-public-key-based-authentication-how-to.html
as this machine has a low priviledge login, I can't do what I want to do.

I need some way to login as the low priviledge account, then

su - sroot

the command I wish to are like these...

ifconfig | grep ^eth0 | awk '{print $5}'

echo "Serial: "; dmidecode | grep -i "serial number";
cat /proc/cpuinfo | egrep -i '(model name|cache)';
echo " ";	
cat /proc/cpuinfo | egrep -i '(cache)';
echo " ";
echo " ";
echo "RAM: "; cat /proc/meminfo | grep MemTotal | awk '{ print $2}';
fdisk -l | grep -i disk;	echo " ";	cat /proc/scsi/scsi | grep -i perc;

Perhaps I should temporarily create a root like account?