expect ssh script issue with if and foreach


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting expect ssh script issue with if and foreach
# 1  
Old 08-23-2010
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:

----------------------------------------------------------------


Code:
#!/usr/bin/expect

set password "testuser"
set chirtr {10.10.136.13 10.10.136.14 10.10.136.15}
set sfortr {10.20.87.13 10.20.87.14 10.20.87.15}
set testcustname [lindex $argv 2]
set testsite [lindex $argv 3]
set custIP [lindex $argv 4]
set custprefix [lindex $argv 5]
set custmode [lindex $argv 6]

send_user "Enter test Customer to activate\(eg.abcd\):"
sleep 5
expect_user {
-re "(.*)\n" {
  set testcustname $expect_out(0,string)
 }
}

send_user "Enter test scrubbing center\(eg. chi or sfo\):"
sleep 5
expect_user {
-re "(.*)\n" {
  set testsite $expect_out(0,string)
 }
}

send_user "Enter Customer IP under activate\(eg. 192.168.1.10):"
sleep 10
expect_user {
-re "-?(0|\[1-9]\[0-9]*)?\\.?\[0-9]*" {
  set custIP $expect_out(0,string)
 }
}

send_user "Enter customer prefix \(eg. 192.168.1.0)\:"
sleep 10
expect_user {
-re "-?(0|\[1-9]\[0-9]*)?\\.?\[0-9]*" {
  set custprefix $expect_out(0,string)
 }
}

send_user "activate or deactivate \(eg. activate)\:"
sleep 5
expect_user {
-re "(.*)\n" {
  set custmode $expect_out(0,string)
 }
}



          ## ACTIVATION ##
if { [$custmode == activate] && [$testsite==chi] } 
foreach host $chirtr
{
puts $host
spawn ssh testadmin@$host
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp xxx\r"
expect "*#"
sleep1
send -- "activate $custIP\r"
}
spawn ssh testadmin@10.10.136.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp xxxx \r"
expect "*#"
sleep 1
send -- "network $custprefix 255.255.255.0\n"
expect "*#"


         ##DEACTIVATION##
if { [$custmode == deactivate] && [$testsite==chi] }
foreach host $chirtr {
puts $host
spawn ssh testadmin@$host
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"
}
spawn ssh testadmin@10.20.87.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"



####################################################

          ##SFO ACTIVATION##

if { [$custmode == activate] && [$testsite==sfo1] }  
foreach entry $sfo1rtr {
puts $entry 
spawn ssh testadmin@$entry
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp yyyyy\r"
expect "*#"
sleep1
send -- "$custIP activate\r"
}
spawn ssh testadmin@10.20.87.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp yyyyy\r"
expect "*#"
sleep 1
send -- "ip address $custprefix 255.255.255.0\n"
expect "*#"


          ##DEACTIVATION##
if { [$custmode == deactivate] && [$testsite==sfo1] }

foreach entry $sfo1rtr {
puts $entry
spawn ssh testadmin@$entry
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"
}
spawn ssh testadmin@10.20.87.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"


expect eof

---------------------------------------------------------------------

---------- Post updated 08-23-10 at 10:58 AM ---------- Previous update was 08-22-10 at 09:07 PM ----------

Reposting the script below. Any help is appreciated.

Last edited by Scott; 08-23-2010 at 03:42 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

2. Shell Programming and Scripting

Expect script ssh passwordless login

how can i use an expect script to do a passwordless ssh login and run a script on a bunch of remote hosts? I do not want to use public key authentication as this would require me to setup the keys on the clients etc.... (2 Replies)
Discussion started by: tdubb123
2 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

Script using SSH with expect command

Hi all, I want to connect to some host with "ssh". I have googled and got some commands of "expect" and "spawn". I was not aware of these commands and tried below script. $ cat auto.sh set host xx.xx.xx.xx set password abcd@1234 set user root spawn ssh $user@$host expect "*?assword:*"... (4 Replies)
Discussion started by: divya bandipotu
4 Replies

5. Shell Programming and Scripting

Expect Script to Automate SSH

How would I write an expect script to automate ssh and what file extention do expect files use? (11 Replies)
Discussion started by: Prodiga1
11 Replies

6. 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

7. Shell Programming and Scripting

SSH Expect Script

Ok, i don't know if anyone else here have had to deal with something like this before, but here's my situation. I have about 1000+ servers I need to log into to do something. What i need to do is to log into each server, go to a certain directory on each of the servers, copy the files that... (3 Replies)
Discussion started by: SkySmart
3 Replies

8. 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

9. Shell Programming and Scripting

Expect script to ssh into MMI

Guys, I know this is tricky.. I'm trying to write a script to pull info from a MMI device. Following script logins into the server and then changes to super user. Opens MMI session and then exits out. I need to run the command under the MMI session "dsp_alarm_span all" Is there any other way to... (2 Replies)
Discussion started by: miltonrods
2 Replies

10. Shell Programming and Scripting

Expect script issue

HI Al, I have written the following expect script: #!/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... (2 Replies)
Discussion started by: Segwar
2 Replies
Login or Register to Ask a Question