linux - Entering Password in su- through loop

08
2014-07
  • dhruv

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

  • Answers
  • MariusMatutiae

    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?


  • Related Question

    linux - Looping Through Subdirectories and Running a Command in Each
  • Bryan Veloso

    I have a set of repositories sorted into directories based on their VCS (Git, Mercurial, SVN). With Subversion I was able to run svn update * in the parent directory and it would loop through each directory and update each repository as expected. That's not the case for Git or Mercurial.

    I would like to come up with a bash script that I can run to do exactly that, loop through directories and either git pull or hg pull in each. I just don't have much bash scripting experience.


  • Related Answers
  • slhck
    for dir in ~/projects/git/*; do (cd "$dir" && git pull); done
    
  • theprivileges

    If you need it to be recursive:

    find . -type d -name .git -exec sh -c "cd \"{}\"/../ && pwd && git pull" \;
    

    This will descend into all the directories under the current one, and perform a git pull on those subdirectories that have a .git directory (you can limit it with -maxdepth).

  • Ole Tange

    If you have GNU Parallel http:// www.gnu.org/software/parallel/ installed you can do this:

    cd ~/projects/git/; ls | parallel 'cd {} && git pull'
    

    This will run in parallel so if some of the git servers' network connection are slow this may speed up things.

    Watch the intro video for GNU Parallel to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

  • slhck

    This should work

    find . -maxdepth 1 -type d -name '.git' -exec sh -c 'cd "{}" && pwd && git pull' \;