jonschipp jonschipp - 2 months ago 20
Bash Question

Shell script to automate SonicWall firewall SSH session not working

I'm trying to write a shell script (Bash) to log into a SonicWall firewall device and issue a command to perform automated backups of the devices ruleset. I prefer to do this in Bash but I will accept a python, perl, except, or applescript solution. If it cannot be done in bash please mention that.


1.) SSH server on firewall is custom, a user name and password has to be specified after issuing a

$ ssh

so no matter what username you issue e.g.

$ ssh

the SSH server still presents a username and password box after

2.) The SSH server is minimal and I cannot use public-keys

I tried using a here-document but it isn't working and it results in an immediate "connection closed by remote host".

The command I need to execute takes the form of this:

export preferences ftp "" "user1" "mypassword" "output.exp"

Connecting gives me this:

$ ssh

Copyright (c) 2010 SonicWALL, Inc.


After a username is issued it brings up the password prompt:


I tried a here-document to no avail.

$ ssh server <<+
export preferences ftp "" "user1" "mypassword" "output.exp"
Pseudo-terminal will not be allocated because stdin is not a terminal.
Connection to closed by remote host.

I tried using echo to pipe in commands too but that doesn't work either.

Typing the commands in manually works just fine.

Any help on this would be greatly appreciated.


As others have suggested, expect is probably what you want to use here.

Here's a short example of how to work with it from bash to get you started:

# +whatever variables you need to use

# Run the expect script from bash
expect_sh=$(expect -c "
spawn ssh $login@$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat results_file\r\" 
expect \"#\"
send \"exit\r\"

# Output or do something with the results
echo "$expect_sh"