Entering Password in su- through loop

0

2

The Scenario is like I have a list of root passwords. But i don't want to keep trying Manually. So i wrote the shell script :

for i in {1..26}

do

su - >>result

done

and all my password are on a file "attempt.txt".

Now on Command Prompt i type the Command :

bash p2.sh < attempt.txt

But It shows errors : "Standard in must be a tty"

So is there some way I can enter these passwords through some codes or commands without manually typing each Of those? Please tell a command-line approach instead of some advanced utility software. I'm in it for learning. Thanks :)

Dhruv Chandhok

Posted 2014-04-09T10:33:41.443

Reputation: 175

Answers

1

The correct syntax is this:

 while read my_pass
 do 
        echo $my_pass | sudo -S command
 done < file_name

Three comments: you cannot use su inside a script file, you will need to use sudo with the -S option which, according to the man,

The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.

Second, if you do not like to write the file_name inside the script, use one of the $n arguments, like $1 if it is the only parameter passed.

Third, are you sure collecting all of your passwords in a single, unencrypted file is such a good idea?

MariusMatutiae

Posted 2014-04-09T10:33:41.443

Reputation: 41 321

No definately it's not a good idea. But In my College Campus Lab there are computers with different passwords, and we are told not to change them. Well since there are 12-14 different password, I dont want to try manually one after the other. So I want that I can test the working one using a script. Ideally they should not keep a password or just give us a list of computer no. and its respective password, but they don't. – Dhruv Chandhok – 2014-04-09T20:40:14.417

Hope this works, I will test it during my lab tomorrow :) – Dhruv Chandhok – 2014-04-09T20:41:27.680

Wasn't able totry it today, But I feel It will work. Thanks :) – Dhruv Chandhok – 2014-04-10T08:00:42.520