bash - Trouble logging in user using su and expect script -
i working on making website class log username , password, , takes page shows grades in class.
the website being run bash script, , hosted on machine users have username , password login.
i have script called calcgrade.sh calculate grades either user logged in, or user passed script argument.
so originally, going use command:
echo -e "$password\n" | sudo -sk -u $user ./website/calcgrade.sh to run calcgrade.sh user of website. however, found out sudo asks password of user currently logged in, not target user trying run command as.
so after reading, found better option use su expect script, can't work. here code expect script (currently username , password hard coded in testing):
#!/usr/bin/expect log_user 0 spawn /bin/su myusername expect "password: " send "mypassword" spawn "./website/calcgrade.sh" interact when run script, doesn't seem log in user su, goes on run calcgrade.sh account, rather user's.
do see wrong script? or can see better way want?
also, problem method calcgrade.sh supposed send output stderr, when run expect script, error messages sent website (the server works sending html website stdout). there way around this, or might better have expect script check su if username/password correct, , if so, run ./calcgrade.sh $user afterwards?
first of all, here's correct way want do:
- give web server user
sudopermissions run./website/calcgrade.shuser, without requiring password. - have web server authenticate user see fit.
- have run
sudo -u someuser ./website/calcgrade.sh, no password required.
now let's @ why approach didn't work:
it's commonly believed su switches user. not case. starts new shell running user.
this means can't spawn su otheruser, let finish, , afterwards spawn calcgrade.sh.
instead have run su otheruser, , send commands shell su starts:
#!/usr/bin/expect log_user 0 spawn /bin/su someuser expect "password: " send "somepassword\n" # wait prompt , send command run expect "$" send "./website/calcgrade.sh\n" interact
Comments
Post a Comment