Search code examples
bashloopssshrootsu

Entering Password in su- through loop


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 :)


Solution

  • You can test the password by making ssh connection in loop:

    #!/bin/bash
    
    sshpass -f<filename> ssh -o 'StrictHostKeyChecking no' -p port user@ip
    

    You should just install sshpass on your system

    This surely works:

    sshpass -p'password' ssh -o 'StrictHostKeyChecking no' -p 22  root@ip
    

    Try this one surely helps you:

    for pass in $(cat /path/to/pass)
    do
       sshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p 22  root@ip
    done 
    

    UPDATE

    So if you want to get pretty output:

    #!/bin/bash
    
    for pass in $(cat /path/to/pass)
    do
        echo -e "Reading $pass..."
        sshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p 22  root@ip
    
        if [ "$?" -ne "0" ]; then
               echo -e "$pass is incorrect\n"
        elif [ "$?" -eq "0" ]; then 
               echo "SSH connection established with pass=$pass"
        fi
    done 
    

    You can also test your passwords with hydra