1

I am writing a script which greps for for multiple files from 3 log files. One of the log file is created as superuser and every time it executes it prompts for password. How do I put it the same script so that I would not have to enter password every time.

for ...
su superuser -c "grep $file /u/spool/ftp/logs/alog>> /u/home/abc/tempo/files.txt"
if [ $? -eq 0 ]; 
   then 
       echo $file >> /u/home/abc/tempo/ftpxfer.txt;
   fi
 done
1
  • Are you sure that you want to do su? It is extraordinary dangerous.
    – SailorCire
    Mar 2 '15 at 12:57
3

Firstly, I will point out that having a superuser password into a file (especially a user-owned script) is a very bad idea, as it allows anyone who gains access to that user the ability to easily gain root privileges.

That being said, you could instead use sudo within the script, which provides an option to read the password from a pipe.

pass='1234abc'
for ... 
    echo $pass | sudo -S "grep $file /u/spool/ftp/logs/alog>> /u/home/abc/tempo/files.txt"
    if [ $? -eq 0 ]; then
        echo $file >> /u/home/abc/tempo/ftpxfer.txt
    fi
done

Alternatively, you could have the whole script run by the root user using sudo - configure sudo (using visudo as the root/super user) to allow the script be run with no password by adding the following into your sudo config file:

%wheel ALL=(ALL) NOPASSWD: /path/to/script

Then your script could be simplified (and not require a plaintext password) thus:

for ... 
    grep $file /u/spool/ftp/logs/alog>> /u/home/abc/tempo/files.txt
    if [ $? -eq 0 ]; then
        echo $file >> /u/home/abc/tempo/ftpxfer.txt
    fi
done
2
  • I am writing a temporary script to grep the files. I am not in sudeors list so cannot use Sudo. Any other option. Mar 2 '15 at 13:19
  • If you know the root password then there's no point in you - or your command - not being in the sudoers file.
    – roaima
    Mar 2 '15 at 23:35
0

If you know root password

ssh-keygen
ssh-copy-id root@localhost
  • accept default for ssh-keygen, do not enter passwd.
  • replace root with proper user if need be.

then replace su with ssh root@localhost in your script.

0

It's as dangerous as @wraeth points out in his answer and his proposal would be the best approach, but you can also use expect.

It allows you to script answers to interactive scripts and is quite useful for this kind of situation if you account for the security concerns. Also, if you have superuser password you could add yourself to the sudoers file and set it up to run your script with privilege and without password.

5
  • Archemar suggestion worked for now. Thank you. I am working on expect script. Appreciate for all answers. Mar 2 '15 at 16:15
  • I tried below expect script. I am able to login but unable to execute anything after login. Mar 2 '15 at 16:41
  • #!/usr/bin/expect -f set username "username" set password "password" set timeout -1 spawn ssh $username@server expect "?assword:" send "$password\r" puts "whoami" expect eof Mar 2 '15 at 16:42
  • You should put your commands inside the expect script or call your script from the expect script. Why did you use an ssh command? Weren't you using just grep? Check http://www.thegeekstuff.com/2010/10/expect-examples/ Mar 3 '15 at 8:55
  • Yup. I need to grep on a remote machine. So trying to login through ssh to remote server and then trying to grep. Mar 3 '15 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.