title: Trouble logging in user using su and expect script
body: <p>I am working on making a website for a class that you log into with a username and password, and then it takes you to a page that shows your grades in the class. </p>
  
  <p>The website is being run with a <a href="https://github.com/jduga002/rapache" rel="nofollow">bash script</a>, and will be hosted on a machine where the users already have a username and password to login. </p>
  
  <p>I also have a script called <code>calcgrade.sh</code> that will calculate the grades for either the user who is currently logged in, or the user passed to the script as an argument.</p>
  
  <p>So originally, I was going to use this command:</p>
  
  <pre><code>echo -e "$password\n" | sudo -Sk -u $user ./website/calcgrade.sh
  </code></pre>
  
  <p>to run <code>calcgrade.sh</code> as the user of the website. However, I found out that <code>sudo</code> asks for the password of the user who is <em>currently</em> logged in, not the <em>target</em> user you are trying to run a command as.</p>
  
  <p>So after some reading, I found a better option would be to use <code>su</code> with an <code>expect</code> script, but I can't get it to work. Here is the code for the <code>expect</code> script (currently username and password are hard coded in for testing):</p>
  
  <pre><code>#!/usr/bin/expect
  
  log_user 0
  
  spawn /bin/su myusername
  expect "Password: "
  send "mypassword"
  spawn "./website/calcgrade.sh"
  
  interact
  </code></pre>
  
  <p>When I run this script, it doesn't seem to log in the user with <code>su</code>, as it goes on to run <code>calcgrade.sh</code> with my account, rather than the user's.</p>
  
  <p>Do you see what is wrong with my script? Or can you see a better way to do what I want?</p>
  
  <p>Also, another problem with this method is that <code>calcgrade.sh</code> is supposed to send some output to <code>stderr</code>, but when I run it with the <code>expect</code> script, the error messages get sent to the website (the server works by sending the html for the website to <code>stdout</code>). Is there a way around this, or might it be better to have the <code>expect</code> script just check with <code>su</code> if username/password is correct, and then if so, then run <code>./calcgrade.sh $user</code> afterwards?</p>