Skip to content

Instantly share code, notes, and snippets.

@rbewley4
Created December 2, 2013 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rbewley4/7758687 to your computer and use it in GitHub Desktop.
Save rbewley4/7758687 to your computer and use it in GitHub Desktop.
Scripting the SonicWall CLI with expect and SSH.
#!/bin/bash
# SonicWall credentials
host="XXXX"
username="admin"
password="XXXX"
# SonicWall CLI configuration
user_pattern="User:"
password_pattern="Password:"
prompt="NSA 3500>"
exit_command="exit"
newline="\n"
# the command to run
command="$1"
# use expect to establish a SonicWall CLI
# session over SSH, and then execute the
# command.
/usr/bin/expect -c "
set timeout 1
spawn ssh $host
# log into SonicWall CLI interface
expect \"${user_pattern}\"
send \"${username}${newline}\"
expect \"${password_pattern}\"
send \"${password}${newline}\"
# execute the command
expect \"${prompt}\"
send \"${command}${newline}\"
# wait for the command to complete, and then
# exit the CLI interface and terminate the
# SSH session
expect \"${prompt}\"
send \"${exit_command}${newline}\"
# the ssh session must be interactive, or else
# the SonicWall will preempt the session.
interact
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment