Expect script issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect script issue
# 1  
Old 04-16-2008
Data Expect script issue

HI Al,
I have written the following expect script:
Code:
#!/bin/ksh
#!/usr/local/bin/expect--
##echo "PLease enter the server name"
##read host
echo "please enter the instance"
read instance
set ##password to be entered right before the script is run##
##/usr/local/bin/expect<<-EOF
cat serverlist.txt | while {read host};
do
/usr/local/bin/expect<<-EOF
spawn ssh -p 2222 $instance@$host
sleep 1
expect "*Password:*"
sleep 1
send "$1\r"
expect "*/export/home/a61359>#*"
send "/usr/local/bin/top -d 2 >> tempfile.txt\r"
expect "*/export/home/a61359>#*"
send "uuencode tempfile.txt tempfile.txt | mailx -s '$host' myemail@mycompany.com\r"
sleep 1
expect "*/export/home/a61359>#*"
sleep 1
send "rm tempfile.txt\r"
sleep 1
expect "*/export/home/a61359>#*"
sleep 1
send "exit\r"
expect eof
done

This script gives me the error
Code:
./ssh.sh[9]: syntax error at line 11 : `<<' unmatched

when I run it. serverlist.txt is a list of servers in teh same directory as the script
How ever when I run the script by commenting out the while loop and taking the server and the instance name from the user it works.
I have already checked the spaces around the /usr/local/bin/expect<<-EOF and the issue persists.
few noteworthy things.
I have not been able to run expect without having the "/usr/local/bin/expect<<EOF" option.
Could somebody please suggest if I am making a syntax error in the while loop or anything else...
Thanks in advance
# 2  
Old 04-16-2008
you can't mix both expect & ksh in same script. write two 2 scripts one reads the hostfile and sends instance,host, password to expect scripts. which does your job. here is the sample. this might not work as i did try to explain. good luck let me know how it goes.

cat yourscript.sh

#!/bin/ksh
echo "please enter the instance"
read instance
set ##password to be entered right before the script is run##
##/usr/local/bin/expect<<-EOF
cat serverlist.txt | while read host;
do
/usr/lib/some_expect.sh $instance $host $password
done

cat /usr/lib/some_expect.sh

#!/usr/local/bin/expect--
spawn ssh -p 2222 $1@$2 $3 # $1 -- Instance $2-- Host $3-- Password
sleep 1
expect "*Password:*"
sleep 1
send "$1\r"
expect "*/export/home/a61359>#*"
send "/usr/local/bin/top -d 2 >> tempfile.txt\r"
expect "*/export/home/a61359>#*"
send "uuencode tempfile.txt tempfile.txt | mailx -s '$host' myemail@mycompany.com\r"
sleep 1
expect "*/export/home/a61359>#*"
sleep 1
send "rm tempfile.txt\r"
sleep 1
expect "*/export/home/a61359>#*"
sleep 1
send "exit\r"
expect eof
# 3  
Old 04-25-2008
Thanks sooo much gopidesaboyina...
This helps a lot....
God Bless
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Expect scripting issue, works interactively when doing commands in cli, does not work in script

Hi; problem may be obvious, simple but I have to say it is somehow not easy to locate the issue. I am doing some word extracting from multiline text. Interacting in CLI seems to work without issues. First step is to add multiline text to a variable. expect1.1> expect1.1> set... (2 Replies)
Discussion started by: aldowski
2 Replies

2. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

3. Shell Programming and Scripting

expect script issue

Hi All, I have the problem to run the commands in expect script from shell script. Mentioned below is the shell and expect script. ##Shell Script## cat bscfile.txt | while read line do NODE=$line ./expect.sh $line $NAME done line= 1st input NAME=2nd input... (1 Reply)
Discussion started by: yadvinder_singh
1 Replies

4. Shell Programming and Scripting

expect ssh script issue with if and foreach

Hi, I am trying to create an ssh script to login to cisco routers and activate/deactivate bgp neighbors if they match certain conditions. I dont think my "if" and "foreach" are working correctly. Any help is appreciated. Below is my script: ... (0 Replies)
Discussion started by: blahblahsomeone
0 Replies

5. Shell Programming and Scripting

help to handle the expect script issue

Hi experts I know the expect script can match the terminal output to run the the following cmd I write a script with expect named "test", I want to run ten "test" with background running, for ((i=1;i<=10;i++) do ./test -n $i done I find all the output of test will print on one... (0 Replies)
Discussion started by: yanglei_fage
0 Replies

6. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

7. Shell Programming and Scripting

Issue - Expect Script.

Hi Gurus,I am trying to automate SFTP using expect.I have written a script that logs into a few boxes(one after the other) and pulls a few files from them.The code:#!/bin/kshcat serverlist.conf|wc -l >> tmp #serverlist.conf contains the list of servers.tmp1=$tmpfor tmp1 in listdo while read... (3 Replies)
Discussion started by: Hari_Ganesh
3 Replies

8. Solaris

Issue with Expect Script for SFTP.

Hi Experts, I am learning expect and wrote the below script for automatic sftp into a server: #!/usr/local/bin/expect -f -d spawn sftp -v test@mumux503 # logs into mumux503 as test user expect "password:" sleep 20 send "test\r"; # sending the password for test... (3 Replies)
Discussion started by: Hari_Ganesh
3 Replies

9. Shell Programming and Scripting

Expect script issue

This is my expect script . set USERNAME set PASSWD set IP set timeout 15 spawn telnet $IP expect "login:" send "$USERNAME\r" expect "Password:" send "$PASSWD\r" expect "Password:" send "$PASSWD\r" expect "*\>" send "show version\r" expect "*\>" send "quit\r" ouput of it (0 Replies)
Discussion started by: robbiezr
0 Replies

10. 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
Login or Register to Ask a Question