I got a script from this question(see the accepted answer), it works great with my own username but when I try to execute it as follows:
sudo bash ./scriptname.sh username password
and give my password, at first it says "can't run as root" , I realized that the script contained this originally So I removed some lines to eventually look like this :
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
export LC_ALL=C
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su "$USERNAME" -c "exit"
expect "Password:"
send "$PASSWORD\r"
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if {[lindex \$wait_result 2] == 0} {
exit [lindex \$wait_result 3]
}
else {
exit 1
}
EOF
but now I'm getting:
spawn su username -c exit
send: spawn id exp6 not open
while executing
"send "password\r""
I saw this question but the answer didn't help me, my script runs fine with my own username but not with root.
I also saw this but it doesn't relate to the job of my script.
So what to do ?
Edit
I have a java application which needs root access to accomplish some tasks And as such I'm running it with sudo
.
At the same time the application needs to execute this script, I can execute a script perfectly from java, I just need the script to be correct.
The application is a server that responds to clients, when the client signs up , the server creates users and groups, I managed to create the users and the groups(that's why the application needs to be run as root).
So what I want to work out here is the authentication, I saw that script and thought of using it but then I had the problem I stated.
So what to do in this situation ?