A man who committed a “disgusting” and “alarming” obscene act outside the bedroom window of a stranger has avoided jail.