Ok I found this:
/usr/local/bin/sshpass -p password ssh admin@1.1.1.1 < ios-cmds.txt
where ios-cmds.txt contains all the commands in separate lines as if I was typing them sequentially.
Also, one of my colleagues suggested using the linux command:
expect
EDIT: One thing to be careful with is, that if the SSH session has never taken place, the certificate exchange part with the switch will make the command fail silently, or do nothing. Connect to the switch first to accept the certificate manually, and then SSHPASS will be happy to login and execute the commands.
EDIT 2: After YBounya's comment, I ended up with this script that basically loops through consecutive IPs and shuts down a range of interfaces, the script receives 'on' or 'off' as an argument to perform either a power on or shut down operation:
#!/usr/bin/expect -f
if { [lindex $argv 0] eq "on"} {
set action "no shut\r"
puts "Turning on switchports\n"
} elseif { [lindex $argv 0] eq "off" } {
set action "shut\r"
puts "Turning off switchports\n"
} else {
puts "No power action found. Provide \"on\" or \"off\"."
exit
}
proc shutPort {ip action} {
spawn ssh admin@192.168.201.$ip
expect {
"(yes/no)" { send "yes\r";
expect { "assword:" { send -- "REAL_PASSWORD\r"; }}}
"assword: " { send -- "REAL_PASSWORD\r" }
"No route to host" { return } ;# switch uses Telnet or just not listen on port 22
"Connection refused" { return } ;# switch is not reachable
"modulus too small" { return } ;# RSA key is not acceptable
}
expect ">"
send -- "en\r"
expect "assword: "
send -- "REAL_PASSWORD\r"
expect -re "\r\n#"
send -- "conf t\r"
expect "(config)#"
send -- "int ran gig1/0/7-48\r"
expect {
"config-if-range" { send -- $action } ; # if previous sends succeeds, enter interface range mode
"marker" { send "int ran gig0/7-48\r"; # interface syntax didn't work
expect {
"config-if-range" { send -- $action }
"marker" { send "int ran fas0/7-48\r";
expect {
"config-if-range" { send -- $action }
"marker" { send "int ran fas0/7-24\r";
expect {
"config-if-range" { send -- $action }
} } } } } } }
send -- "exi\r"
send -- "exi\r"
send -- "exi\r"
expect eof
}
for {set i 42} {$i < 51} {incr i} {
shutPort $i $action
}