Shell Scripting
more_horiz
close

on 20th September 2017 / by anoop.john
Youtube does not allow audio files to be uploaded. However you can easily convert an audio file into a video file using the given audio file and a fixed image as a background. The following works on Ubuntu and any variants of GNU/Linux. For converting a single mp3 file you can use the following.ffmpeg -v quiet -i "picture.jpg" -i input.mp3 -shortest -acodec copy output.mp4; done For converting a batch of files in a folder you can use the followingfor i in *.mp3; do ffmpeg -v quiet -i "picture.jpg" -i "$i" -shortest -acodec copy "`sed 's/mp3/mp4/g'<<<$i`"; done You will need to install ffmpeg in your GNU/Linux system to do this though Youtube Ubuntu Shell Shell Scripting Video Processing FFMpeg Leave a reply Your email address will not be published. Required fields are marker *
close

on 23rd October 2015 / by Anoop John
When you try to set up a local development environment from a drupal dump which includes just the code base and the database without the Drupal files folder you will run into the issue of missing files. Drupal would think that it still has all the files in the Drupal files folder and would create links / process images as usual. This would however lead to a lot of unexpected errors in a lot of unexpected places. If you really do not care about the content in these files and want to recreate the missing files here is how you do this. First copy over index.php and create test.php. Replace menu_execute_active_handler(); with custom code to run the following query and output the list of files in the system. SELECT REPLACE(uri, 'public:/', '') AS path FROM file_managed WHERE uri LIKE ('public://%') ORDER BY filemime ASC Create a folder test inside the webroot. Save the list of files as files.txt inside this folder. Now copy over the following code into a bash script inside the same folder #!/bin/bash while read line do #echo $line; path="../sites/default/files$line" if [ ! -f "$path" ]; then #echo "File does not exist - $path" extn=`echo $line | sed 's/^.*\.//'` new="" case "$extn" in jpg|JPG|jpeg|JPEG) new=test.jpg ;; png|PNG) new=test.png ;; doc|DOC|docx|DOCX) new=test.doc ;; pdf|PDF) new=test.pdf ;; xls|XLS|xlsx|XLSX) new=test.xls ;; ppt) new=test.ppt ;; txt) new=test.txt ;; zip) new=test.zip ;; svg) new=test.svg ;; mp4) ;; *) echo "$extn" ;; esac fi if [ ! -z "$new" ]; then cp "$new" "$path" chown apache:apache "$path" echo "Created $path" fi #echo $path; done < "./files.txt" The code is pretty self-explanatory for a programmer. If you are not one, we can help you solve this problem :-). Happy Hacking. Drupal Drupal Development Shell Scripting Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 19th December 2014 / by sandeep.sasikumar
The following is the shell script that will shutdown all computers in the network. This script will be really help full for system administrators who need not want to check all systems, whether all the systems in the office has been shutdown properly when the employees leave the office. The script will check all the active ips in the network and write the those ips to a file. It also login to each system and execute the shutdown command. The things that are to be done before running the script is to 'Make sure your public-key has been added in the authorized_keys file in .ssh folder of root'. This allows the script to login to each system by the method of public-key authentication rather than prompting for password. And also the 'StrictHostKeyChecking no' will avoid all prompts during ssh. #!/bin/bash ####Script to find all active ips in the network and shutdown all the systems by logging into it. nmap -sP 192.168.1.0/24 | grep 192 | cut -d " " -f 5 | sed '1d' | sed '$d' > activeips.txt while read activeips do echo "$activeips" ssh -o "StrictHostKeyChecking no" root@$activeips 'shutdown -h now' done < activeips.txt Shell Scripting Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 11th December 2014 / by sandeep.sasikumar
This is a small shell script that will help to detect ip conflicts in a network. The notify-send command, used in the script, will enable a popup if any ip conflict is detected. You can run this script in cron. You will get a notification as pop up, if there is any ip conflict in the network. #!/bin/bash # Script to check ipconflict # Grep the word "Dup" and enable a pop up to show that there is ip conflict sudo arp-scan -I eth2 -l | grep DUP if [[ $? == 0 ]]; then notify-send "Ip-conflict detected" fi Shell Scripting Bash GNU/Linux Leave a reply Your email address will not be published. Required fields are marker *
close

on 11th November 2014 / by webmaster
If you are a GNU Linux user it is very likely that you would already know about the command top that allows you to see the memory and processor usage by each process in your system. However you would see multiple processes for applications like Chromium or Firefox for each of the tabs that are open in them. Similarly you could see apache run multiple processes if you have it configured to do so. What if you want to see the total memory usage for each application and not by each process. Here is a small bash script to help you do the same. Copy the following and save as ls-mem (or whatever name you wish to name it as) and then add it to a folder which is in your $PATH variable. It could be ~/bin or /usr/local/bin based on whether you have permissions to these folders and based on whether the folder is in the $PATH variable. #!/bin/bash ps -A --sort -rss -o comm,pmem,rss | awk ' NR == 1 { print; next } { a[$1] += $2; b[$1] += $3; } END { for (i in a) { size_in_bytes = b[i] * 1024 split("B KB MB GB TB PB", unit) human_readable = 0 if (size_in_bytes == 0) { human_readable = 0 j = 0 } else { for (j = 5; human_readable < 1; j--) human_readable = size_in_bytes / (2^(10*j)) } printf "%-20s\t%s\t%.2f%s\t%s\n", i, a[i], human_readable, unit[j+2], b[i] } } ' | awk 'NR>1' | sort -rnk4 | awk ' BEGIN {printf "%-20s\t%%MEM\tSIZE\n", "COMMAND"} { printf "%-20s\t%s\t%s\n", $1, $2, $3 } ' | less Make sure that you set the execute bit for the script that you have created (chmod 555 ls-mem) and you are all set. Happy scripting Shell Scripting GNU Linux BASH Scripting Leave a reply Your email address will not be published. Required fields are marker * Ershad (not verified) access_time 22 Feb 2019 - 15:25 for (j = 5; human_readable < 1; j--) human_readable = size_in_bytes / (2^(10*j)) division at this line fails when size_in_bytes is 0 with the following error, we could move this loop to a new else part of the succeeding if condition awk: cmd. line:9: (FILENAME=- FNR=221) fatal: division by zero attempted corrected version - https://gist.github.com/ershad/428514b68b4bdf2c570d webmaster access_time 22 Feb 2019 - 15:25 In reply to for (j = 5; human_readable < by Ershad (not verified) Thanks Ershad for the correction. Have updated the article with the corrected code. Bernardo (not verified) access_time 22 Feb 2019 - 15:25 In reply to Thanks for the correction by webmaster Did you break the script with your update? webmaster access_time 22 Feb 2019 - 15:25 In reply to Did you break the script with by Bernardo (not verified) Thanks for catching it. This has been fixed. Add new comment
more_horiz
close

on 26th February 2014 / by webmaster
Lines of code is typically indicative of effort that goes towards software development. So how do you count lines of code in your PHP application (or for that matter any specific types of files in any application). You can't manually open and count files, that would be a big waste of time. The *nix shell has a powerful set of tools that can help you count lines without having to do it manually. find . -name "*.php" | xargs wc -l | awk '{print $1}' | awk '{total = total + $1}END{print total}' The find command finds files named *.php (you can replace this with other patterns or sets of patterns with -o and -a). These files are passed to wc using xargs. wc prints out the list of files with the number of lines in each. The tabular output is then processed using awk to separate out just the column of line counts. This is then again processed using a bit of awk scripting. I hope you got the general idea here on how these commands work to give you the number of lines of code in your PHP files. Happy coding. Shell Scripting Bash BASH Scripting Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 23rd December 2013 / by webmaster
One of the challenges with working with dynamic websites is that you have to keep fighting malicious users who regularly sap your server capacity with rogue crawling of your site. To do this you would have to monitor and analyze the traffic patterns on the server regularly. You would definitely want to do this when you have load spikes on the server and you wish to find out the IP addresses, user agents and the specific URLs which resulted in these spikes. This is all the more relevant in Drupal sites where a rogue bot can take down the site when proper DDOS mechanisms are not set in place. The latest copy of the script can be downloaded from https://github.com/zyxware/misc-utils/tree/master/ls-httpd You can copy the script to /usr/local/bin or into some folder which is in your $PATH variable on the server. Remember to configure the script with the path to your apache access log. You can update the default value of the variable log_file to wherever your apache log file is located. Also do note that the script was written for the specific log file format used in our servers. You might want to tweak the awk parameters if your apache log file uses a different format. Alternatively you can use the following as your apache log file format in apache.conf LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined and then ensure that your log format is set up as combined in your virtualhost configuration CustomLog ${APACHE_LOG_DIR}/access.log combined The following are some example usage patterns ls-httpd url 1000 will find top URLs in the last 1000 access log entries ls-httpd ip 1000 will find top IPs in the last 1000 access log entries ls-httpd agent 1000 will find top user agents in the last 1000 access log entries ls-httpd url 17: will find top URLs from 17:00:00 to 17:59:59 ls-httpd url 17:2 will find top URLs from 17:20:00 to 17:29:59 ls-httpd url 17:21 will find top URLs from 17:21:00 to 17:21:59 ls-httpd url 17 will find top URLs in the last 17 access log entries :-) Hope you find this useful. Drupal Free Software Apache Shell Scripting BASH Scripting DDOS Contributions Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 10th August 2013 / by Anoop John
I was trying to get a list of MIDI files play in banshee 2.0 in an old computer. The system had Ubuntu 11.04 and banshee was able to play MIDI files except that it was not able to detect the end of each track and continued playing the same track with silence as the only output and infinitely playing the same track. So whenever there was a long silence I had to manually change to the next track. I did that a few times but then decided to write a small script to solve the problem. Banshee supports passing commands via the command line. It also supports querying information about the currently playing track from the command line. However it was not able to get the length of the currently playing track from the command line. For this I had to use another utility (smfsh - sudo apt-get install smf-utils) Now smfsh is an interactive command-driven frontend to libsmf, useful for modifying MIDI files by hand and for querying information about MIDI files. Because it opens an interactive shell I had to write a small input file that passes the required command to the shell and then exit the shell. The complete script is as follows. This is released under GNU/GPL v3 and is available from the zyxware github account under the misc-utils repository. #!/bin/bash debug=0 # Infinite loop while : do # Get the path to the current file cur_file=`banshee --query-uri | cut -c 13-10000` # Get the length of the current track using smfsh since banshee is not # able to stop at the end of the midi files cur_track_length=`smfsh $cur_file &1 | grep Length | awk '{print $4}' | cut -d. -f1` # Get the current position in the current track cur_track_pos=`banshee --query-position | awk '{print $2}' | cut -d. -f1` if [[ $debug -eq 1 ]]; then echo "Playing $cur_file at $cur_track_pos / $cur_track_length seconds" fi if ! [[ "$cur_track_length" =~ ^[0-9]+$ || "$cur_track_pos" =~ ^[0-9]+$ ]]; then banshee --next else # If banshee has been playing for longer than the duration of the current track then # play the next track if [[ $cur_track_pos -gt $cur_track_length ]]; then banshee --next fi fi sleep 5 done You will need to create an in.txt file in the same folder with the following contents as well length quit You can save the text file as play-midi.sh or something like that and then run it in the background and that should be all. The script will check if banshee has overrun the currently played midi file and if so switch to the next track. Ubuntu Utilities Free Software Shell Scripting GNU Linux BASH Scripting Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 14th May 2013 / by naveen.pl
I had a requirement where I had to execute shell command from PHP. The requirement was that I had to find details of a domain using commands like whois and then store this information into files in a folder. The only input parameter for the process was the name of the domain which was passed on from the Drupal site. The PHP function (shell_exec) was designed to do things like this. Example code: // Site url $site_url = www.example.com // Sample code to test if the code works fine on your local machine. // Instead of the code given below you can add code to create directory // for the user depending upon the condition. // Keep in mind that each call will open a new shell instances. $outping = shell_exec("ping -c2 -w2 $site_url") ; // You can place the outping inside pre tag to see the output clearly. echo "$outping"; PHP Shell Scripting Leave a reply Your email address will not be published. Required fields are marker *
close

on 11th January 2013 / by sandeep.sasikumar
Here is a script to help you find empty directories located under a specified parent-folder. The file folders.txt contains the list of folders that are to be passed as input to the script. The script will now check all the folders mentioned in the file folders.txt, and give the list of folders that are empty. The output can either be seen at the terminal or it can be passed to a file via a redirector. Here is the code for the same: #!/bin/bash # Script to find empty directories # Read the name of folders from the folders.txt file # path to the folder #find . -type d -empty will find all empty directories path=/path/to/the/parent/folder while read folder do echo "$folder" cd $path/$folder empty=`find . -type d -empty` echo -e "$empty" done < folders.txt Linux System Administration Shell Scripting Leave a reply Your email address will not be published. Required fields are marker *