title: Linux - Bash to login to a machine and then become root
body: <p>I am trying to login to Linux box with my regular user and then become root using a bash script. My user is in the sudoers file so it can become root.</p>
  
  <p>The normal process would be the following:</p>
  
  <pre><code>$ssh regularuser@machine
  regularuser@machine's password:
  [regularuser@machine ~]$
  [regularuser@machine ~]$ sudo su -
  [sudo] password for regularuser:
  [root@machine ~]$
  </code></pre>
  
  <p>I searched all over to do this but all I could find doesn't do exactly what I need. I also tried using #!/usr/bin/expect but it doesn't work.</p>
  
  <pre><code>===========================================================================
  </code></pre>
  
  <p>My working bash (access.sh) is as follows <strong>but it asks me for the password after issuing the "sudo su -" command</strong>:</p>
  
  <pre><code>#!/bin/bash
  machine=$1
  ssh -t regularuser@$machine "sudo su -"
  
  $ sh access.sh machine
  regularuser@machine's password:
  [sudo] password for regularuser:
  [root@machine ~]# exit
  logout
  Connection to machine closed.
  
  ===========================================================================
  </code></pre>
  
  <p>Attempt #1 - When I tried the following I got "<strong>/usr/bin/expect: No such file or directory</strong>"</p>
  
  <pre><code>#!/bin/bash
  machine=$1
  echo "Password?"
  read -s password
  
  expect -c '
      spawn ssh regularuser@'"$machine"'
      expect "Password: "
      send '"$password\r"'
  '
  </code></pre>
  
  <p>Attempt #2 - Same result</p>
  
  <pre><code>#!/bin/bash
  machine=$1
  echo "Password?"
  read -s password
  
  /usr/bin/expect &lt;&lt; EOF
  spawn ssh regularuser@$machine
  expect "Password: "
  send "${password}\r"
  EOF
  </code></pre>
  
  <p>Attempt #3 - Doesn't work either. It gives me "<strong>unexpected EOF while looking for matching `''</strong>"</p>
  
  <pre><code>#!/bin/bash
  machine=$1
  echo "Password?"
  read -s password
  
  ssh -t regularuser@$machine "echo $password | sudo -S su -"
  
  
  $ sh access.sh machine
  Password?
  regularuser@machine's password:
  bash: -c: line 0: unexpected EOF while looking for matching `''
  bash: -c: line 1: syntax error: unexpected end of file
  Connection to vm-all-all-ad-test-packages closed.
  </code></pre>
  
  <p>I cannot do "sshpass -p "$password" ssh root@$machine" either because the password of my regular user is different from root's</p>
  
  <p>I tried pretty much everything out there but with no success.</p>
  
  <p>Any ideas?!!</p>