# in pseudocode no less! get $deviceIP ping $deviceIP or report it as not responding and exit ssh $deviceIP if ssh error then if ssh error is 'connection refused' then telnet $deviceIP or report error and exit do work with telnet connection else report error and exit end if end if do work with ssh connection