Don't assume stdin is a TTY

Check whether stdin is a TTY before performing TTY operations that would
cause an exception to be raised. Makes sudo usable e.g. when invoking
commands via ssh.