Bash script failed with expect on cisco routers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script failed with expect on cisco routers
# 1  
Old 05-31-2012
Question Bash script failed with expect on cisco routers

Hi all,

I use a bash script which use expect to connect throught ssh and run command on a cisco router. The ssh connection with expect work fine, but the first command on the cisco router failed,
I try to run the command in error by hand and it work fine... Smilie

the first part of the script read a config file to get user and password for ssh connection.

Code:
#!/bin/bash

export WORKDIR="/foo/bar/"

function parameter()
{
 cd $WORKDIR
 if [ -r Default.properties ]; then
          sed 's/ *\#.*//' Default.properties | egrep -v '^$' > fifo 
          while read ligne ; do    # Lecture du fichier de configuration (et suppression des commentaires).
            echo $ligne

            if echo $ligne | egrep '^[   ]*host[         ]*=[   ]*.+$' >/dev/null ; then
              host=$(echo $ligne | sed 's/^[     ]*host[         ]*=[   ]*//')
              echo $host
            elif echo $ligne | egrep '^[         ]*login[        ]*=[   ]*.+$' >/dev/null ; then
              login=$(echo $ligne | sed 's/^[    ]*login[        ]*=[   ]*//')
              echo $login
            elif echo $ligne | egrep '^[         ]*password1[     ]*=[   ]*.+$' >/dev/null ; then
              password1=$(echo $ligne | sed 's/^[         ]*password1[     ]*=[   ]*//')
              echo $password1
            elif echo $ligne | egrep '^[         ]*password2[     ]*=[   ]*.+$' >/dev/null ; then
              password2=$(echo $ligne | sed 's/^[         ]*password2[     ]*=[   ]*//')
              echo $password2
            fi
          done < fifo
          rm fifo
 fi
}

function connection
{
expect <<EOD
#!/usr/bin/expect
spawn ssh $2@$1
expect "$2@$1's password: "
send "$3\r"
expect eof
interact
EOD

expect <<EOD
#!/usr/bin/expect
spawn en
expect "Password: "
send "$5\r"
expect eof
interact
EOD


}

NEW_CONFI=$1

if [ -z "$NEW_CONFI" ]; then
  error "Veuillez indiquer le NEW_CONFI en parametre (ex: 1234)"
fi


parameter
connection $host $login $password1 $1 $password2

when I run it i get the following error :

Code:
foo01-bar> spawn en
couldn't execute "en": no such file or directory
    while executing
"spawn en"

I read the post 165501 but in my case passing argument to expect work fine, the ssh connexion is ok.

If some has any idea ..
Sorry for my bad english Smilie
# 2  
Old 05-31-2012
is "en" the enable command on the router? You probably don't want spawn, but send.
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 06-01-2012
I try "send en\r" instead of spawn en but it didn't work too.

edit : Yes the en command is an alias for the enable password on cisco (like sudo on linux)

Last edited by bedomon; 06-01-2012 at 04:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cisco, 2 ssh logins for expect /bash

HI all i need to connect to about 900 cisco routers and switch to do some configs changes. the issue i am having is that half the devices have one set of username and password and the other half have another username and password. From expect or bash script i can ssh into a device and make... (0 Replies)
Discussion started by: quintin
0 Replies

2. UNIX for Advanced & Expert Users

Bash script + expect

im very happy to back for this forum I have servers with alias of double dns extentions: sample: servera.test.com servera.test1.com serverb.test.com serverb.test1.com I need to login to that severs and executing the set of commands if test.com failed then try to login via... (0 Replies)
Discussion started by: prakash0106
0 Replies

3. Shell Programming and Scripting

Bash Script Locate Word Failed

I'm using CentOS 7 and I would like to know of a way to read each line and search for a specific word. For example, if it finds the word "Fail" it sends it to a variable, and pipes it to a folder on the network. What would be the best way in making this work in a Linux environment? I wrote a... (1 Reply)
Discussion started by: SysAdminRialto
1 Replies

4. Shell Programming and Scripting

Bash script with expect

Dear all Hi I want use expect in bash so that we can not use these with each other /bin/bash. With. /usr/bin/expect How can use these with on script or how can call a script from other script #!/bin/bash clear echo "================================== " echo "Enter your Esxi IP"... (3 Replies)
Discussion started by: Baber
3 Replies

5. Shell Programming and Scripting

Expect script to show cisco configs

I know there are better ways to do this. I prefer snmp. I do not have the proper perl modules loaded on the platorm. Snmp isnt loaded on the platform. Telnet is not an option. I need to write an expect script to pull cisco equipment configs. The following code is executed once I gain... (0 Replies)
Discussion started by: popeye
0 Replies

6. Shell Programming and Scripting

Perl Telnet cisco routers

Hi all i have little problem finding solution about simple telnet script .... i have 250 routers and on some i have different password and on some they ask just for password no username. So for example 1,2,3 have username and password (user,password) 4, and 5 have different username and password... (0 Replies)
Discussion started by: IvanMP
0 Replies

7. Shell Programming and Scripting

expect in bash script

Hi, I'm writing a shell script that calls a few commands that prompt the user for two simple yes/no questions. if the answers are consistent (the first is a yes, the second is a no), what would my expect script look like? Google is only giving me answers for scripts where I telnet or ssh. right now... (3 Replies)
Discussion started by: js741
3 Replies

8. Shell Programming and Scripting

Expect/telnet/testing tacacs on a cisco

At times I find the need to test that the tacacs port 49 is open. The code below works but is painfully slow because I have to wait on the timeouts. Examples of possible responds router1#telnet 10.11.20.14 49 Trying 206.112.204.140, 49 ... Open route1#telnet 10.11.19.14 49 Trying... (1 Reply)
Discussion started by: popeye
1 Replies

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