ZakCheb's technical blog.

Follow @ZakCh3b
17 July 2019

Automating CLI interactions with expect

I decided today to automate my interaction with my home router, a DSL-2750U, I usually use the web interface, but found out that a telnet management session is possible, of course, only accessible from the LAN, after some google search, I discovered this great program named expect, that automate interaction with any other CLI based programs.

#!/usr/bin/expect
spawn telnet Router 
# Show hosts of DSL-2750U home router.
expect "Login: " { send "USERNAME\r" }
expect "Password: " { send "PASSWORD\r" }
expect "> " { send "lanhosts show all\r" }
expect "> " { send "exit\r" }
interact

This simple script “expect” a value from the router, then push information, in our case the credentials and the command to execute. Now, I can simply parse the hosts with grep, and have a better overview of my network.

tags: