A burglar who targeted two care homes and attempted to stab staff with a pen has been jailed after being caught on CCTV.