If you want to write a program that talks to your router, search for a program called expect
- it will serve you well. It uses the serial port of the router to do so.
In answer to your basic question, no, you can't write a program that is downloaded to a router that will run on it.