Script to log into unix box and do a set of activities

Hi all,
I want to automate a set of activities i am doing daily.the activities in the order are:
1.loging in to the unix box.
2.sudo su - tsiap, give pwd
3. cd appsrv
4. cd log
5. run the below cmd one by one, if you find any query which has run for more than 5 secs, open the particular SQL log(sqllog_clrdg5q1 for queue1 & sqllog_clrdg5q2 for queue2) using less command(less <respective log file> and search with the process id to get the SQL.

command or query:cat sqllog_clrdg5q1 | grep SQL_ROUNDTRIP_TIME | cut -d' ' -f4,4,3,3,7,7,8,8 | sort -k 2,2n | tail -5

example ouput:#7835 27.892056 01/18/08 00:01:32

I open that particular process #7835 and copy the query in to word document.

I have to dod this for 4 servers ...so it is tiring and boring

Any idea's and pointers on how this can be implemented are welcome

Thanks in advance,
Cybersandex.

Anything you can do at the prompt, you can do by script. The parts which are not well-specified will have to be better specified, or done by hand.

for server in server1 server2 server3 server4; do
  ssh $server "sudo -u tsia for db in sqllog_clrdg5q1 sqllog_clrdg5q2; do
    grep  SQL_ROUNDTRIP_TIME appsrv/log/$db |
    cut -d' ' -f4,4,3,3,7,7,8,8 |
    sort -k 2,2n | tail -5
    # TODO: automate the remaining part here?
  done"
done

Notice that the repeated cd commands and the cat are superfluous; you can simply grep with a path name to the file.