0

this is my script"

#!/usr/bin/expect -f
set USERNAME "user"
set PASSWORD "password"
set ENABLEPSW "enable-password"
set HOST "host-fqdn"
spawn ssh -o StrictHostKeyChecking=no $USERNAME@$host-fqdn
expect "Password: "
send "$PASSWORD\n"
expect "*# "
send "config t\n"
expect "*(config)# "
send "vlan 250\n"
expect "(config-vlan)# "
send "name NEWNAME\n"

Basically I want the script to ssh, avoid cert checks, log in, make the router go in config mode, then go to vlan 250 config mode and finally rename the vlan. What is happening is that the script reaches the config-vlan mode but immediately exits the script before issuing that last command. No matter what I do it never throws that last command. The last "expect" check is properly configured.

root@shell:/# ./routerconnect.sh
spawn ssh -o StrictHostKeyChecking=no USERNAME@HOST-FQDN
User Access Verification
Password:
Cisco Nexus Operating System (NX-OS) Software
TAC support: http://www.cisco.com/tac
Copyright (C) 2002-2015, Cisco and/or its affiliates.
All rights reserved.
The copyrights to certain works contained in this software are
owned by other third parties and used and distributed under their own
licenses, such as open source.  This software is provided "as is," and unless
otherwise stated, there is no warranty, express or implied, including but not
limited to warranties of merchantability and fitness for a particular purpose.
Certain components of this software are licensed under
the GNU General Public License (GPL) version 2.0 or
GNU General Public License (GPL) version 3.0  or the GNU
Lesser General Public License (LGPL) Version 2.1 or
Lesser General Public License (LGPL) Version 2.0.
ROUTER# config t
Enter configuration commands, one per line. End with CNTL/Z.
ROUTER(config)# interface vlan 250
ROUTER(config-vlan)# 
root@shell:/#
Barmar
  • 741,623
  • 53
  • 500
  • 612
ds3010
  • 1
  • 1
  • 1
  • Shouldn't you have some more `expect` and `send` commands to wait for the next prompt and save the configuration? – Barmar Nov 18 '16 at 22:22
  • Sorry I am not an expert in shell scripting or tcl. Not sure what you mean by more expect and send commands. – ds3010 Nov 18 '16 at 22:55

1 Answers1

1

You need to wait for the name command to finish. So try like this:

send "name NEWNAME\n"
# The last command finishes only when you see the next prompt.
expect "(config-vlan)# "
pynexj
  • 19,215
  • 5
  • 38
  • 56