Hello Ovais,
for this kind of things you could use the expect library under tcl.
http://expect.nist.gov/
this is thought to be able to manage interactive logins
only warning: when you shut tunnel1 the branch is still reachable ?
www.activestate.com activetcl 8.4.x or later should have an expect library
you need to build a list of devices ip addresses that will be indexed in a for cycle and in the for body there the instructions for performing the required actions
I used it and it works well. I had used a 5.21 version of porting to windows
if you have a linux box it should be even easier
commands are
expect "sername"
send $user
expect "assoword"
send $pass
during configuration the expect should match only the hostname
Hope to help
Giuseppe