What the hell. If the device you are logging into supports sudo then it probably supports bash or python too. In that case a better solution is to
create a script on the target device and then call that script over ssh, using key-based authentication. You do not need expect. Expect is mainly used on devices with a rigid terminal UI that do not support scripting and require interactive password authentication.