Expect Proves More Effective than Ansible in Legacy Environments

Today, I am writing a scheduled backup task for an old Huawei switch S7703. After struggling with Ansible for a while, I ultimately resolved it using Expect. In legacy environments and with old devices, traditional methods still work best.

During the use of Ansible, various network modules like ce/net were called, and the YAML execution and configuration were split in multiple places. The impressive AI, when I explicitly requested to use native SSH for direct connection, still insisted on its own way, sometimes claiming there were issues with VRP, and at other times saying the module paramiko was unavailable. The scripts provided either resulted in connection errors or execution errors, so I ended up pulling out the old method of using Expect + Telnet.

I modified an old Expect script, making it concise and clear, which also made modifications easy.

The log_file appears in pairs; the first instance starts the logging, and the second instance stops it.

#!/usr/bin/expect -f
set timeout 30
set host "192.168.1.254"
set user "username"
set password "passw0rd"
set ts [clock format [clock seconds] -format "%Y-%m-%d_%H-%M-%S"]]
set logfile "/backup/netconf/${host}_${ts}.log"

log_file $logfile
spawn telnet $host
expect {    "Username:" {        send "$user\r"        exp_continue    }    "Password:" {        send "$password\r"        exp_continue    }    "<Core-Sw1>" {        send "screen-length 0 temporary\r"        expect "Info: The configuration takes effect on the current user terminal interface only."
        send "display current-configuration\r"    }    timeout {        send_user "Telnet connection timed out.\n"        exit 1    }    eof {        send_user "Telnet connection closed by remote host.\n"        exit 1    }}
send "quit\r"
expect eof
log_file

Expect is like a chef’s knife that a Chinese cook takes everywhere, while Ansible is like the wall of kitchen tools found in a European kitchen.

There is a question online asking why the United States was able to send astronauts to the moon in 1969, but cannot land on the moon today. This is probably the reason. Technical tools are renewed every year, but the old masters are nowhere to be seen.

Expect Proves More Effective than Ansible in Legacy Environments

Leave a Comment