title: How to execute local script as root in remote servers?
body: <p>I need to execute a shell script with root privileges in remote servers. Those servers can't be connected directly via SSH by root user. Thus, I have to connect ssh as normal user first and then change it to root before execute the script. The user and root passwords are parameters stored previously so keyboard interaction is not an option. In that way I'd need a non-(human)-interactive script like:</p>
  
  <pre><code>#!/bin/sh
  
  username=admin
  password=admin123
  hosts={host1..host50}
  
  for hostname in $hosts ;
  do
     ssh $username@$hostname sudo 'bash -s' &lt; ./test.sh &gt; results.txt
  done
  </code></pre>
  
  <p>How can I do that? In my current code I need to type password for admin user connection and then I have an error like: <code>sudo: no tty present and no askpass program specified</code>. Maybe a command like <code>expect</code> is needed but I don't have much expertise there.</p>
  
  <p>Thanks for your help.</p>
  
  <p>PS: I can't use SSH Keys and administration changes are not allowed in these servers.</p>