jonschipp jonschipp - 2 days ago 5
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.

Problems:

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

$ ssh server.com


so no matter what username you issue e.g.

$ ssh admin@server.com


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 "ftp.server.com" "user1" "mypassword" "output.exp"


Connecting gives me this:

$ ssh admin@server.com


Copyright (c) 2010 SonicWALL, Inc.

User:


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

User:user1
Password:


I tried a here-document to no avail.

$ ssh server <<+
user1
mypassword
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
exit
+
Pseudo-terminal will not be allocated because stdin is not a terminal.
Connection to 10.1.1.1 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.

Answer

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:

login=root
IP=127.0.01
password=helloworld
# +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"
Comments