One of the best things about books online is the free code.
If you look up dbcc showcontig you will see a routine on the help page that retrieves the table output of the dbcc and then runs index defrag against any index fragged at or above 30%
this is easily modified to be a rebuild or a reindex.