Using Perl to Replace Text in Multiple Files
You can search and replace text inside multiple files using Perl. This is how you do it:
$ perl -i -pe 's/Windows/Linux/;' test*
This command will search for the word ‘Windows’ inside all files that begin with ‘test’. When it finds one, it will substitute it with the word ‘Linux’. This is useful if you have multiple files that contain the same text. Remember that you can use regular expressions to make searching flexible.
Tips: SSL Certificate Checker
If you have one too many SSL certificates to manage, there is one nifty program made with Bash that can determine a certificate’s validity.
SSL Certificate Expiration Check can do that for you and send the results to email. The program’s “send to email” feature allows the user to simply add it to cron to check the certificates and send an alert to the administrator if any certificate (from file or website) falls below the set expiration date.
Tip: Detect Network Connectivity in Bash
There are times that we are required to check hundreds of servers for network connectivity, and what command comes first in mind when testing network – ping. To help out other system and network administrators out there, here is a quick bash script to ping servers.
#!/bin/bash
SERVERS=server.txt
for i in $(cat $SERVERS)
do
ping -c2 $i > /dev/null
if [ $? -ne 0 ]
then
echo "$i is DOWN"
else
echo "$i is UP"
fi
sleep 3
done
Tip: How to Place Pause in Bash Scripts
There some scripts that need human interaction from time to time from confirming certain actions or to warn users that they are about to do something stupid ( “ rm -rf / “, perhaps?). In any case, putting a pause break in a Bash script is a very nifty trick to learn.
This is an example of how to use the Bash built-in command read to put pauses in scripts:
#!/bin/bash
find /home -type f -name "*.txt"
read -p "Press [enter] key to delete *.txt files in /home or CTRL+C to exit"
find /home -type f -name "*.txt" | xargs rm -v
This example script will run the find command and display the results. If user presses the Enter key, the script will remove all files that were found, while pressing CTRL+C will terminate the script.
Here is another variation of the the script:
#!/bin/bash
find /home -type f -name "*.txt"
echo "Press [enter] key to delete *.txt files in /home or CTRL+C to exit"
read contscr
find /home -type f -name "*.txt" | xargs rm -v
Tip: Add User and Generate Password Script
Hey guys!
Finally, I found time to update this lovely blog of mine. And luckily, I have one super cool simple script to share. This script will help a lot of system administrators in adding user accounts, very useful if you have about a hundred servers to login to and execute useradd and passwd again and again. This is a bash script, so no need to install packages and easy to understand.
Here goes the code.
#!/bin/bash
for USER in $(cat /tmp/users.txt) do
$(useradd $USER)
###Remove the space between 8 and )
PASSWORD=$(openssl rand -base64 8 )
###passwd [double dash] stdin
echo $PASSWORD | $(passwd –stdin $USER)
echo “$USER $PASSWORD” | mail -s “Your Account Info” $USER@company.com
sleep 3
done
Here is a quick summary of the code per line:
for USER in $(cat /tmp/users.txt) do
This line begins the for loop and /tmp/users.txt should be a text file containing the list of usernames to be added one username per line. The value of $USER
$(useradd $USER)
This line will begin by executing the command useradd. If the script fails at this point because the username already exists, it will proceed with changing the password of the username.
###Remove the space between 8 and )
###passwd [double dash] stdin
PASSWORD=$(openssl rand -base64 8 )
echo $PASSWORD | $(passwd --stdin $USER)
This line generates an 8-byte random password using openssl and pipes it to command passwd using –stdin option so the generated password can be passed.
echo "$USER $PASSWORD" | mail -s "Your Account Info" $USER@company.com
This line sends an email to the user containing the username and password, assuming that the email address is the same as the username.
This script can be further enhanced (imagination is limitless!) and feel free to share your script here.
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- The_ALL
on How to Increase Wi-Fi Signal - chyrica
on Cebu Pacific Sucks - chyrica
on Cebu Pacific Sucks - wallpat
on Cool Ubuntu-Filipino Wallpapers - celebrity fuck you
on Diablo 3 Coming Soon








