Computing Staff
  • 0

Batch File Sudo/Su Login

  • 0

Hi,
I want to create a DOS Batch file:
1. That login on 100 sun solaris/Unix servers.
2. search syslog.0, syslog.1, syslog.2…n so on
3. Then remove them.
To use RM command i have to login with SU.

Please help me to create that script.

I have already used a login script

@echo off
c:\Plink.exe -ssh -l xyz 192.168.1.155 -pw “123456” -m input.txt >> result.txt

but i can not remove those file because to remove i have to login with “su”.
and “su” can not login remotely.
i also unable to use sudo, because ask for password, I i dont know how to enter password in sudo script.

Please help

Share

2 Answers

  1. startouch1:

    I assume you are the administrator and
    since you have to do this on 100 servers,
    every day, you would rather automate ?

    Let’s build a solution for you step-by-step.

    1. The solaris shell command to remove all
    syslog.* files is


    find "/" -name "syslog.*" -print -exec rm {} \;
    


    2. To remove syslog files, you will need
    to be superuser. You can NOT login as su
    remotely, but you can supply
    the supreuser password using a HERE DOCUMENT.
    So, the solaris script is

    su root << "EOF"
    su_password
    EOF
    find "/" -name "syslog.*" -print -exec rm {} \;
    


    3. If you put the above script in your DOS file
    input.txt, and call the plink command as follows,
    it will automate this for one server.

    c:\Plink.exe -ssh -l xyz 192.168.1.155 -pw “123456” -m input.txt >> result.txt

    4. You now want to automate for
    100 solaris servers (I assume that means
    a lot of solaris servers).

    Create a file C:/ServerList.txt as follows.

    192.168.1.155\txyz\t123456\tsu_password
    

    \t is tab.
    First field is IP address of the solaris server,
    second remote login,
    third remote password,
    fourth superuser password.
    Each server entry – one per line.

    5. Let’s now write a biterscripting
    ( http://www.biterscripting.com )
    script to automate this for all Solaris servers
    using the input from file C:/ServerList.txt.


    # Script SolarisLogs.txt
    var str list, serverentry, address, rlogin, rpswd, supswd, plinkcmd
    set $wsep = "\t"
    # Read server list.
    cat "C:/ServerList.txt" > $list
    # Get the first server entry.
    lex "1" $list > $serverentry
    # Process server entries one by one.
    while ($serverentry <> "")
    do
        # Extract the fields.
        wex -p "1" $serverentry > $address
        wex -p "2" $serverentry > $rlogin
        wex -p "3" $serverentry > $rpswd
        wex -p "4" $serverentry > $supswd
    
        # Compose the input.txt file.
        echo "su root << "EOF"" > input.txt
        echo $supswd >> input.txt
        echo "EOF" >> input.txt
        echo "find "/" -name "syslog.*" -print -exec rm {} \;" >> input.txt
    
        # Compose the plink command.
        set $plinkcmd="c:/Plink.exe -ssh -l "+$rlogin+" "+$address+" -pw ""+$rpswd+"" -m input.txt >> result.txt"
    
        # Execute the plink.cmd.
        system -s $plinkcmd
    
        # Get the next server entry.
        lex "1" $list > $serverentry
    done
    


    6. Save this script in file
    C:/Scripts/SolarisLogs.txt on your local
    DOS system, from where you will kick off
    this activity. You can now run this script
    manually from biterscripting with this command.


    script "C:/Scripts/SolarisLogs.txt"
    


    7. Of course, you do not want to
    run this manually every day. So,
    schedule this script to run at a time daily
    when the servers are least in use,
    using cron or task scheduler,
    with the following command.


    "C:/biterScripting/biterScripting.exe" script "C:/Scripts/SolarisLogs.txt"
    


    That completes the solition. It was a good
    challenge. I have not tested it. You may need
    to do some trial-and-error during testing.
    But I hope that this, at least, provides a
    good starting point.


    And yes, if you improve upon this solution,
    you should re-post it for the benefit of
    other over-worked Solaris administrators.

    • 0
  2. One error in my above post. The cron or task scheduler command should be


    "C:/biterScripting/biterScripting.exe" "C:/Scripts/SolarisLogs.txt"
    


    You should not have the word script in that command.

    • 0