Any good tutorials or sites for expect scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Any good tutorials or sites for expect scripting?
# 1  
Old 08-26-2008
Any good tutorials or sites for expect scripting?

In a nutshell here is what I am trying to do:

I have a file with a list of hostnames. I want to read in the file and ssh to each hostname on the list. Do some commands, logout of the server and go onto the next on the list.

I am trying to find a site that is a good tutorial or examples so I can learn how to do more than just the basics with expect.

Thanks

Robert
# 2  
Old 08-26-2008
# 3  
Old 08-26-2008
Thanks...but I am looking for help with expect scripting and reading in files, not the actual use of SSH.
# 4  
Old 08-26-2008
From reading this -- I don't see a reason to have to use expect.

You can execute commands on a remote system through ssh.
Code:
chris@host01> ssh host02 uptime
Password:
 11:25am  up 5 day(s), 14:17,  5 users,  load average: 0.07, 0.03, 0.02

I'm sure you noticed I still entered a passwd. Ok so you setup your ssh keys between hosts so you don't have to enter a passwd.

So you parse your file to get your list of hostnames.
I have no idea the particulars of you situation -- So I am making stuff up.
Code:
cat file
somethinghere host1 somethingthere
somethinghere host2 somethingthere
somethinghere host3 somethingthere
somethinghere host4 somethingthere

FILE=your_file_here
HOSTS=`awk '{print $2}' $FILE`

for x in $HOSTS;do
	ssh $x "date;uptime;df -k /"
done

This will execute date, uptime, and the df on all four hosts...

I use a script I made called forhost to do similar tasks. I work often on hundreds of machines in series, executing one -- or more commands on the host then moving on to the next host.

$1 is the command/commands you want to run and $2 is you list of boxes...
Here is how to use it:
Code:
chris@host01> cat tmp.lst
host-ap551
host-db501
host-ww501
host-ww502

Code:
chris@host01>./forhost "uptime;date" tmp.lst
host-ap551 is alive
SYSTEM :: host-ap551     1:20pm  up 16 day(s),  5:47,  11 users,  load average: 3.09, 3.02, 2.99
Tue Aug 26 13:20:12 CDT 2008

host-db501 is alive
SYSTEM :: host-db501     1:20pm  up 16 day(s), 6 hr(s),  40 users,  load average: 4.45, 4.35, 4.62
Tue Aug 26 13:20:14 CDT 2008

host-ww501 is alive
SYSTEM :: host-ww501     1:20pm  up 9 day(s),  1:55,  3 users,  load average: 0.41, 0.38, 0.39
Tue Aug 26 13:20:16 CDT 2008

host-ww502 is alive
SYSTEM :: host-ww502     1:20pm  up 9 day(s),  1:54,  24 users,  load average: 5.52, 5.41, 4.09
Tue Aug 26 13:20:21 CDT 2008

Or....
Code:
chris@host01>./forhost "uptime;date" " host-ww503 host-ww504"
host-ww503 is alive
SYSTEM :: host-ww503     1:22pm  up 16 day(s),  3:36,  9 users,  load average: 1.20, 1.37, 1.32
Tue Aug 26 13:22:07 CDT 2008

host-ww504 is alive
SYSTEM :: host-ww504     1:22pm  up 12 day(s), 14:17,  20 users,  load average: 0.68, 0.89, 1.32
Tue Aug 26 13:22:09 CDT 2008

Or.....
Code:
chris@host01>./forhost "uptime;date" "`grep db tmp.lst`"
host-db501 is alive
SYSTEM :: host-db501     1:24pm  up 16 day(s),  6:04,  41 users,  load average: 4.34, 4.35, 4.56
Tue Aug 26 13:24:13 CDT 2008


And here is the script.....

Code:
#!/bin/ksh
#### Determine if $1 $2 are present
if [[ -z $1 || -z $2 ]];then
        print "Useage: $0 \"command 1;command 2;command 3\"  list of servers"
        print "command may be one or more commands eperated by ;"
        print "any spaces in command sequence require wrapping in \" \""
        print " "
        print "the list of servers can be a file. If so any line containing # will be omitted"
        print "or the list can be a few hosts wrapped in \" \""
else
        continue
fi
#### Determine if $2 is a file or not
if [[ -f $2 ]]
then
        LIST=`grep -v '#' $2|sort`
else
        LIST=$2
fi
for SYSLIST in $LIST
do
        ## Check and see if $x is alive
        #UP=`ping $SYSLIST 2`
        if ping $SYSLIST 2
        then
                #### Execute a command on SYSTEM defined in $LIST
                ssh -q $SYSLIST "echo \"SYSTEM :: `echo $SYSLIST`   \c\";$1;echo ' '"
        else
                echo "$SYSLIST is Down? "
        fi
done

# 5  
Old 08-27-2008
Thanks that has helped a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Websites with good tutorials

Hey guys, i am new in Shell programming and i would like to know some good links and websiites which has good tutorials and a wide range of examples on how the commands can be used. those which i have found just provide a brief coverage on the topic and mostly do not give proper examples of how the... (5 Replies)
Discussion started by: gregarion
5 Replies

2. UNIX for Dummies Questions & Answers

Good remote sites to get graphics from?

Hello, I am starting ym first webpage and I am learning how to get images from a remote unix site to my unix account. But I don't know of any good sites to get some. Anyone know of any? (3 Replies)
Discussion started by: werty7373
3 Replies

3. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

4. Shell Programming and Scripting

Help for good books in Shell Scripting

Hi, I am knew to Shell Scripting. Can someone please tell me some good books for Shell Scripting which explains in a very simple language and covers all the topics nicely. Thanks, Shubh. (3 Replies)
Discussion started by: shubhranshu
3 Replies

5. AIX

AIX routing and TCPIP. Anyone find any good Tutorials?

HI All, Does anyone out there know of any good online tutorials for the basics of routing, gateways, networking with AIX, preferably the more recent versions like 5.2/5.3? Using commands such as netstat, smit mkroute, etc. to connect to boxes on a network, and load software with networked... (1 Reply)
Discussion started by: jeffpas
1 Replies

6. UNIX for Advanced & Expert Users

Good sites to learn EMC symmetrix

i need to learn this very quick. i'm not trying delve deep into it. just enough to do basic things. does anyone know of any good sites to visit?? (1 Reply)
Discussion started by: TRUEST
1 Replies

7. Shell Programming and Scripting

Need a good scripting book

Just a quick request guys As you might have guessed I've just started getting involved in Unix The guys and the boss in the unix team (not with them yet) have given me some projects to do at my request. Some of which involve scripting. The work is paying for me to go on a scripting... (2 Replies)
Discussion started by: w33man
2 Replies

8. UNIX for Dummies Questions & Answers

any tutorials on simple scripting?

i'm not looking for anything that deals with "if-then" scripts. i'd like something simple on how to run a series of processes. for example the following: 1. ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/tar_files/ 2. lcd / 3. get pkgsrc.tar.gz 4. bye 5. cd /usr 6. rm -rf pkgsrc 7. cd... (3 Replies)
Discussion started by: xyyz
3 Replies

9. UNIX for Dummies Questions & Answers

unix tutorials or good sites

whats a good site for unix learning? are their any tutorials on this site? (1 Reply)
Discussion started by: mfbww
1 Replies
Login or Register to Ask a Question