I used expect to execute some command with sudo on the remote host via ssh. The script is as follows:
spawn ssh -q ${user}@${host} expect "*?assword:" send "${pass}\r" expect "${user}@" send "sudo ls\r" expect "*?assword:" send "${pass}\r" expect "${user}@" send "exit\r" interac
It works fine for the first time, but when I executed it sequentially, an error occurred. This is because sudo will not expire immediately, therefore, if sudo some command twice in a short time, the second one may not need a password, so the second send "${pass}\r" in the above script failed!
So, how can we detect this and avoid sending a password when sudo does not expire? thanks!
source share