Author Archives: admin

In bash how to get the field before the last one?

In bash how to get the field before the last one

In bash how to get the field before the last one

In bash how to get the field before the last one?

Parse field before last in bash shell script – An interesting piece of code today which could hit any system administrator who handles a lot of data parsing and processing, is how to extract some data from a comma seperated file when one of the data fields contains a comma as well….

If you have tried this yourself you will know that the output you were hoping for will be screwed. As an example here is some data –

doe,john,9 london road,"neighbour jack jones",2016,semi detached
jones,jack,10 london road,"neighbour smith,jane",2016,semi detached

As you can see the neighbour field on the 2nd record has listed jane smith with a comma in the data (even though it has quotes around it bash cut ignores that) making it hard for you to do a simple cut to get the date moved in (2016 – the 2nd to last field)

[root tmp]# cat /tmp/m5 | cut -d"," -f5
2016
jane
[root tmp]#

So… how to get around it and parse field before last you ask?

Well a command called ‘rev’ will do it for you! Lets first show you what it does –

[root tmp]# cat /tmp/m5 | rev
dehcated imes,6102,senoj kcaj ruobhgien,daor nodnol 9,nhoj,eod
dehcated imes,6102,enaj,htims ruobhgien,daor nodnol 01,kcaj,senoj
[root tmp]#

As you can see its simply reversed everything on each line character by character. but now you can see that the date you want is in field 2, so lets grab it –

[root tmp]# cat /tmp/m5 | rev | cut -d"," -f2
6102
6102
[root tmp]#

And then we need to put it back in the right sequence by reversing it again back to its original form –

[root tmp]# cat /tmp/m5 | rev | cut -d"," -f2 | rev
2016
2016
[root tmp]#

Nothing in bash is hard.. whats hard is knowing the commands in order to do it!

If you enjoyed this post or found it useful then please share it to your friends or social networks. Also check out more bash tips around the bashworkz website.

Teamviewer for Android (Teamviewer Server for Android)

Teamviewer for Android (Teamviewer Server for Android)

Teamviewer for Android (Teamviewer Server for Android) – As many readers of my site will know I am the happy owner of a Minix X8-H Plus TV Box. My main use of the box is for watching TV using XBMC, Mobdro, FilmOn and a variety of other programs, and I also play some games like Dead Trigger 2, Asphalt 8, and most recently Megapolis.

Im sure I will do a seperate review at some time of Megapolis, but for now lets just say it is a town/city bulding game which has tasks and challenges in order to pass achievements and progress. Many of these tasks are time based which makes having the game on my TV box slightly restricting if I am out or at work.

So it was time to see if there was some way I could remote control my TV box from anywhere. I have used Teamviewer in the past for both personal and business use, but when I had looked previously there was not the functionality on Android for it to act as the Host for a remote connection. (or maybe I had just not found it…)

Anyway yesterday I managed to find a seperate app called Teamviewer Host.

A small app which links to your Teamviewer account and will add your Android device to your list of remote servers.

Your remote hosts will appear on your list something like this (small list as im not a huge teamviewer user) –

Teamviewer for Android - Teamviewer Host Device List

Teamviewer for Android – Teamviewer Host Device List

Then when you remote into your Android TV Box (or other android device) you get something like this –

Teamviewer for Android - Teamviewer to Minix X8-H Plus

Teamviewer for Android – Teamviewer to Minix X8-H Plus

As you can see on the top left it allows you to transfer files. The dashboard screen shows your device model etc, you can see list of processes running on your android device, and as the screen above shows you can remote control your device.

Full list of features from their Google Play page :
• Remote control
• Real-time screen sharing
• View device information
• Transfer files (Back and forth)
• App list (Uninstall apps)
• Process list (Stop processes)
• Push and pull Wi-Fi settings
• Store confidential information into the device clipboard
• Secured connection with 256 bit AES session encoding

Easily access your Android devices unattended, anywhere, and any time.

One annoying thing I havent worked out yet, is that it does not auto start the Teamviewer Host app process when you boot up your android box. Im sure there is a way if you root your device, and maybe there is some other app that can auto start programs you want (let me know if you know of something like this!!)

Download Link – https://play.google.com/store/apps/details?id=com.teamviewer.host.market

If you found this article useful then check out more Android related articles on the site, including lists of my Favourite Android Apps

How to test if lftp has worked in bash script

How to test if lftp has worked in bash script

How to test if lftp has worked in bash script

How to test if lftp has worked in bash script

Hi everyone, one of the things I tend to do a lot when writing Bash Scripts is to create a lot of small and simple checking scripts for various functionality.

I have been using lftp quite frequently with a client of mine for some time now, but one of the errors more junior coders do is to just check $? to establish whether the previous command has worked or failed.

The famous test bash code is –

if [ $? -eq 0 ]
then
echo "success"
exit 0
else
echo "failed"
exit 1
fi

BUT like I said that tests what exit code the previous command has sent you. In the case of lftp (and MANY other commands) all it is telling you is that the lftp command started, NOT whether the file you wanted to send to your remote host has actually worked or not.

So here is a very simple script that will give you the correct exit code for you to then decide how you want to proceed if for example your lftp has failed. I have called the script lftp_check.bash

#!/bin/bash
 #
 # Utility to check lftp to ensure data has been transferred
 #
 # $1 - lftp output file
 #
 # Script will exit with 0 for success, 1 for error, 2 for no logfile found
 #
 # Example -
 #
 # [root@serv tmp]# echo test > test.txt
 # [root@serv tmp]# lftp "sftp://${FTP_USER1}:${FTP_PASS1}@${FTP_HOST1}" -e "cd ${FTP_DEST1};put text.txt;bye" > /tmp/lftp_output.txt 2>&1
 # [root@serv tmp]# cat /tmp/lftp_output.txt
 # cd ok, cwd=/destination_dir
 # put: /tmp/text.txt: No such file or directory
 # [root@serv tmp]# lftp "sftp://${FTP_USER1}:${FTP_PASS1}@${FTP_HOST1}" -e "cd ${FTP_DEST1};put test.txt;bye" > /tmp/lftp_output.txt 2>&1
 # [root@serv tmp]# cat /tmp/lftp_output.txt
 # cd ok, cwd=/destination_dir
 # 5 bytes transferred
 # [root@serv tmp]#
 #
 # Written by Malc on 14.07.2016
 #

LFTP_FILE="$1"

if [ -f "${LFTP_FILE}" ]
 then
 grep "bytes transferred" "${LFTP_FILE}" > /dev/null
 if [ $? -eq 0 ]
 then
 echo "lftp successful"
 exit 0
 else
 echo "lftp error"
 exit 1
 fi
 else
 echo "$LFTP_FILE not found"
 exit 2
 fi

So a little explanation –

1) Firstly you already have your lftp command, you need to capture the output from it so the check script can analyse it

lftp "sftp://${FTP_USER1}:${FTP_PASS1}@${FTP_HOST1}" -e "cd ${FTP_DEST1};put text.txt;bye" > /tmp/lftp_output.txt 2>&1

2) Now you can tell lftp_check.bash to use /tmp/lftp_output.txt and tell you if the transfer has worked or failed

/usr/local/bin/lftp_check.bash /tmp/lftp_output.txt

The script itself is very simple, the output shows the text ‘bytes transferred‘ which means your file has been sent (or received). You can then check the exit code $? like the example above to determine if your lftp has worked (exit code 0), or failed (exit code 1), or it couldnt find your lftp output file to read in the first place (exit code 2).

For my purposes that is all I need, but it would not be hard to take the script a step further to capture the size of the data sent and match it to the file you were sending and compare them.

Install and configure Varnish 4 – Varnish Cache for WordPress

Install and Configure Varnish Cache for WordPress

Install and Configure Varnish Cache for WordPress

Install and configure Varnish cache for WordPress on Centos 6

Todays post will show you how to install and configure Varnish 4 cache on your VPS (or dedicated) server to use on your WordPress site.

A fairly common issue WordPress Webmasters have is the larger their wordpress websites get, the slower and slower it also gets. If they use a smaller VPS server for their sites they start to hit memory problems, apache load problems, and a range of other issues.

WordPress has a large number of caching plugins available, and believe me I have tried them all at one stage or another with varying degrees of success.

Varnish is a much more generic caching tool which can be made to work with WordPress using my guide below. You can use it alongside all your regular wordpress caching plugins as well if you like so you can effectively test it live, and if it doesnt help you then you can simply unattach it from your site again and continue as you were.

1) Prepare the Varnish 4 Repository

On Centos 6 you need to install the Varnish repository like this –

rpm -Uvh http://repo.varnish-cache.org/redhat/varnish-4.0/el6/noarch/varnish-release/varnish-release-4.0-4.el6.noarch.rpm

On Centos 7 Varnish 4 is available through the EPEL repository which you possibly already have installed. If you dont then do this –

rpm --import https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
rpm -Uvh https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

2) Install Varnish cache for WordPress

As always now you have the repo installed getting varnish on there is simple –

yum install varnish

3) Configure Varnish cache for WordPress

Varnish configuration is stored in a file called /etc/varnish/default.vcl. vcl is effectively a language in itself, but its fairly standard in its formation of conditional statements. If you have a basic html website then Varnish comes with a default vcl file which will probably work and speed up your site massively. However WordPress uses cookies all over the place which stop Varnish from working its magic to its full potential.

I cannot claim writing this varnish config for wordpress, but I can claim I have added and adjusted bits of it to suit my particular server and what runs on it –

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "81";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}

# Only allow purging from specific IPs
acl purge {
"localhost";
"127.0.0.1";
}

# This function is used when a request is send by a HTTP client (Browser)
sub vcl_recv {

#Force specific urls or vhost urls to pass to the backend (not cached)
if (req.http.host == "XXXXXXXXXXXXXXXXXXXXXXXX" || req.http.host == "YYYYYYYYYYYYYYYYYYYYYYYY") {
return(pass);
}

# Normalize the header, remove the port (in case you're testing this on various TCP ports)
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");

# Remove has_js and CloudFlare/Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");

# Allow purging from ACL
if (req.method == "PURGE") {
# If not allowed then a error 405 is returned
if (!client.ip ~ purge) {
return(synth(405, "This IP is not allowed to send PURGE requests."));
}
# If allowed, do a cache_lookup -> vlc_hit() or vlc_miss()
return (purge);
}

# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}

# --- WordPress specific configuration

# Did not cache the admin and login pages
if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") {
return (pass);
}

# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");

# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");

# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");

# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");

# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ "^ *$") {
unset req.http.cookie;
}

# Cache the following files extensions
if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
unset req.http.cookie;
}

# Normalize Accept-Encoding header and compression
# https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
if (req.http.Accept-Encoding) {
# Do no compress compressed files...
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
unset req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}

# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
return (pass);
}
if (!req.http.cookie) {
unset req.http.cookie;
}

# --- End of WordPress specific configuration

# Did not cache HTTP authentication and HTTP Cookie
if (req.http.Authorization || req.http.Cookie) {
# Not cacheable by default
return (pass);
}

# Cache all others requests
return (hash);
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (fetch);
}

# The data on which the hashing will take place
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}

# If the client supports compression, keep that in a different cache
if (req.http.Accept-Encoding) {
hash_data(req.http.Accept-Encoding);
}

return (lookup);
}

# This function is used when a request is sent by our backend (Nginx server)
sub vcl_backend_response {

if (bereq.url ~ "XXXXXXXXXXXXXXXXXXXXXXXX" || bereq.url ~ "YYYYYYYYYYYYYYYYYYYYYYYY") {
return(deliver);
}

# Remove some headers we never want to see
unset beresp.http.Server;
unset beresp.http.X-Powered-By;

# For static content strip all backend cookies
if (bereq.url ~ "\.(css|js|png|gif|jp(e?)g)|swf|ico") {
unset beresp.http.cookie;
}
# Don't store backend
if (bereq.url ~ "wp-(login|admin)" || bereq.url ~ "preview=true") {
set beresp.uncacheable = true;
set beresp.ttl = 30s;
return (deliver);
}

# Only allow cookies to be set if we're in admin area
if (!(bereq.url ~ "(wp-login|wp-admin|preview=true)")) {
unset beresp.http.set-cookie;
}

# don't cache response to posted requests or those with basic auth
if ( bereq.method == "POST" || bereq.http.Authorization ) {
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}

# don't cache search results
if ( bereq.url ~ "\?s=" ){
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}

# only cache status ok
if ( beresp.status != 200 ) {
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}

# A TTL of 2h
set beresp.ttl = 2h;
# Define the default grace period to serve cached content
set beresp.grace = 30s;

return (deliver);
}

# The routine when we deliver the HTTP request to the user
# Last chance to modify headers that are sent to the client
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.x-Cache = "MISS";
}

# Remove some headers: PHP version
unset resp.http.X-Powered-By;

# Remove some headers: Apache version & OS
unset resp.http.Server;

# Remove some heanders: Varnish
unset resp.http.Via;
unset resp.http.X-Varnish;

return (deliver);
}

sub vcl_init {
return (ok);
}

sub vcl_fini {
return (ok);
}

Config Download linkHERE

Some key parts to note in the config above are –

vcl_recv – This section is used when varnish receives a request from your browser. It is here where we remove unwanted cookies WordPress generates
vcl_deliver – It is here where varnish decides if it has any cached content for the request and sends it to the browser
vcl_backend_response – makes decision if it needs to ask the backend server for data (example wordpress admin pages we would not want to cache). We can also determine here if we want to exclude anything else from being cached (example may be that you have multiple websites but only want to keep your main one in varnish cache)

4) REQUIRED Server config for Varnish Cache for WordPress

Dont start Varnish yet, there are a few other things to do….

vi /etc/sysconfig/varnish

In here you can set the size of the cache to whatever is comfortable for your server by editing VARNISH_STORAGE_SIZE=XXXM

Also as you are wanting to put Varnish in front of your web traffic you will want to set VARNISH_LISTEN_PORT=80

IMPORTANT –
You might remember in the default.vcl file I had set the backend server to be on port 81 (near the top of the vcl file). This means you will also need to adjust your apache/nginx backend server vhost(s) to run on that port.

IF you have done all the above correct (and if my notes are right…) then we are almost there. You might want to set Varnish to auto start on server reboot like this –

chkconfig varnish on

After updating your backend vhosts port you will want to restart apache –

service httpd restart

Then you are ready to start Varnish –

service varnish start

Some stats are always nice…. so you got these to have a play with

varnishstat – updating screen to show counts of your cache hits and misses
varnishlog – if you really want to dig into some more detail

Want some more Varnish addons for monitoring or additional tools?

http://varnish-cache.org/extras/index.html#extras

Things to note –

– If you are upgrading from Varnish 3 to Varnish 4 then there are some changes you will need to make to your default.vcl file, it is not a straightforward upgrade and expect everything to work
– Check the Varnish website for much more detail about each of the sections in the config for Varnish. As I said above the config I have shown you here was not created by me, but works with WordPress. It can be added to for individual needs and server configurations

If you found this post useful then subscribe to the site for new posts which I do every week or so. Also check out my other TUTORIALS and BASH scripting hints and tips.

How to stop people trying to hack your root logon

How to stop people trying to hack your root logon

How to stop people trying to hack your root logon

How to stop people trying to hack your root logon

A sometimes neverending hassle for a Unix Administrator is how to stop people from trying to hack your root logon. There are many approaches to this, none of which are particularly difficult to do, so here is a checklist of some of the methods I have used.

Firstly you need to be aware HOW to find out someone is trying to hack your root logon (or other logon actually). This can easily be found on Centos 6 (in fact every Linux distribution) in the /var/log/btmp file.

Slightly strange however is that you cannot simply do a ‘more‘ or ‘cat‘ of this file. To see its contents you do this –

strings /var/log/btmp

The other slightly annoying thing is you cannot see date and timestamps in it. Oh well, you know the attempt was made which is the main thing. You can also see from the output of ‘strings’ the usernames that were attempted. Now bear in mind the ip addresses or hostnames it shows COULD BE YOURS, so dont just go ahead and block them all on your office firewall (or your server iptables) without being sure that you are not about to block yourself or your staff.

So anyway here is my quick checklist of things you can do once you have identified ip addresses or hostnames that are undesirables.

1) BLOCK THEM IN IPTABLES

The sledgehammer approach is to add a rule similar to this to your /etc/sysconfig/iptables file –

-A INPUT -s 1.2.3.4 -j DROP

and then restart iptables to take effect.

The potential issue with this is you could be being attacked by multiple servers on a network (can be catered for with the above line 1.2.3.4/32 to drop a standard class C ip network), or even an undesirable hacker datacentre with multiple ip addresses

2) CHANGE YOUR SSH PORT

A cleverer method to disrupt at least the less sophisticated hack attempts is to change your server from using the standard ssh port 22 to something different. One thing to note here is just to be a bit careful when doing this as a mistake in changing SSH could stop you from accessing your server.

So the file you need to edit is /etc/ssh/sshd_config

Quite near the top you will see a line like this –

#Port 22

And yes it is normally commented as shown above. So remove the comment, and change your port to be something else (try and use a higher number over 1024 I suggest to stay away from other system reserved ports). Use something like 22222, 22111, 11122 or something else that you can remember.

Once you have edited and saved that file restart sshd like this –

service sshd restart

Now when you log in your server remember to use your new port in your terminal client.

3) DISABLE DIRECT ROOT ACCESS

In my view this is by far the most subtle method, and your attackers wone even know you have done it, and will continue to try and try and try, and keep getting ‘invalid password’, ‘invalid password’, ‘invalid password’, while you laugh to yourself knowing they stand no chance.

Once again this method involves editing /etc/ssh/sshd_config if you scroll down you will find something like this –

#PermitRootLogin yes

And yes its once again commented, which basically is just showing you the default value. Uncomment it, and change to no like this –

PermitRootLogin no

and save the file.

IMPORTANT – What you now need to do is to make sure you have a seperate logon for yourself to be able to log in your server with !!! To create a user do this –

useradd -d /home/user1 user1

and set a good password on it like this –

passwd user1

Then restart sshd like this –

service sshd restart

Now you can log in as your new user and then do ‘su -‘ to take you to root. Then you can watch your /var/log/btmp file and not worry that anyone can log in as root any more.

SUMMARY –

So there are my most used methods to add that little bit of extra security to your server. If you are interested to learn more hints and tips then check out my other Linux or Bash tutorials HERE or subscribe to my email list in the sidebar to get new tips to your inbox as I write them!

How to easily disable a wordpress plugin

How to easily disable a wordpress plugin

How to easily disable a wordpress plugin

How to easily disable a wordpress plugin

Todays post will show you how to disable a wordpress plugin easily. This is particularly useful if a plugin update does not work or causes issues.

I was a bit stuck to decide what to write about, until I logged in my WordPress Admin panel and saw that Flowplayer5 plugin needed an update. As usual I went to plugins and selected to update it which all worked fine until I then tried to create my new post. I got the 500 apache server error page.

So time to look through the apache logs to see why. I came across this error –

PHP Parse error: syntax error, unexpected '[', expecting ')' in ............./wp-content/plugins/flowplayer5/admin/settings/class-register-settings.php on line 52

Checking the code there looked nothing immediately obvious wrong with the code, but my website was down, so my main task was to get it running again!

The easiest thing to do is simply disable the wordpress plugin. But how you ask, when you cant get to the wordpress admin page???

Fortunately I have ssh access to my server, but it is still possible to do this if you have some control panel like cpanel etc.

Use your file manager. All thats needed is to rename the specific plugin folder from within wp-content/plugins directory. In my case today I simply did this –

[root@vs3 plugins]# mv flowplayer5 flowplayer5.tmp

WordPress then cannot see the plugin and doesnt load it. Then you can speak to the developer of the plugin to fix the issue (in the above instance with flowplayer there was a coding issue on my version of PHP).

Once you have a fixed version, you can extract it and move it to the plugins directory and if everything goes well it will work again.

What I have also noticed is when you rename the plugin to .tmp then it also marks the database that the plugin is inactive. When you copy in the new one you will then have to activate it in wordpress admin again.

How to easily disable a wordpress plugin

How to easily disable a wordpress plugin

You should now have WordPress working again.

Check out my other WordPress hints and tips HERE or subscribe to my blog for email updates and new tutorials, using the form on the right side at the top.

How to convert a wav to mp3 using ffmpeg

How to convert a wav to mp3 using ffmpeg

How to convert a wav to mp3 using ffmpeg

How to convert a wav to mp3 using ffmpeg

Todays post I will show you how to convert a wav to mp3 format. If you use Soundcloud you will often come across free downloads of tracks from up and coming producers who want to spread their music to more people, or from established artists who use a free track as some additional promotion for their future releases or events.

Most people these days would agree that mp3 is still the most accepted format for most audio players and devices, although there are many other formats available like aac, mp4 (stems), flac, and indeed wav which all have their place.

ffmpeg is a hugely useful media conversion tool which I have spoken about before, so today I will show you how easy it is to take your wav file and convert it to an mp3.

1) Follow my guide to install ffmpeg HERE. As you can see I use a static version as its totally simple to install, and has all the functionality I have ever needed.

2) I got this great track from Soundcloud HERE – https://soundcloud.com/abstractillusion/abstract-illusion-isolation-vip-free-download-mastered-wav and as you can see when you download it, it is a wav file. So upload it to your linux box (or ffmpeg works on windows). Anyway get it in a directory you can get to and get to your command prompt.

Bit of promo for Abstract Illusion here is his free download track –

3) ffmpeg has 100s of options but for this task it is simple. First we will look at the wav file we have downloaded –

ffmpeg -i "Abstract Illusion - Isolation VIP.wav"

Output –

ffmpeg version 2.0.1 Copyright (c) 2000-2013 the FFmpeg developers
built on Sep 24 2013 05:31:18 with gcc 4.8 (Debian 4.8.1-10)
configuration: --extra-cflags=-I../static/include --extra-ldflags='-L../static/lib -static' --enable-gpl --enable-version3 --enable-static --disable-shared --disable-debug --enable-runtime-cpudetect --enable-libmp3lame --enable-libx264 --enable-libspeex --enable-libvorbis --enable-libvpx --enable-libfreetype --enable-libxvid --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-gray --enable-libopenjpeg --disable-ffserver
libavutil 52. 38.100 / 52. 38.100
libavcodec 55. 18.102 / 55. 18.102
libavformat 55. 12.100 / 55. 12.100
libavdevice 55. 3.100 / 55. 3.100
libavfilter 3. 79.101 / 3. 79.101
libswscale 2. 3.100 / 2. 3.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 3.100 / 52. 3.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, wav, from 'Abstract Illusion - Isolation VIP.wav':
Duration: 00:06:14.81, bitrate: 2116 kb/s
Stream #0:0: Audio: pcm_s24le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s32, 2116 kb/s
At least one output file must be specified

The key things to note are the Hz and the kb/s highlighted above which show the quality the file was recorded in. If (for example) your file was recorded at 128 kb/s there would be no point in converting it to a 320 kb/s file (it will not make the file better quality)

4) ok so now to convert your wav to mp3. Here is how you do it –

ffmpeg -i "Abstract Illusion - Isolation VIP.wav" -acodec libmp3lame -ac 2 -ar 44100 -ab 320k -f mp3 "Abstract Illusion - Isolation VIP.mp3"

-ac 2 – audio channels 2 for stereo as it said the file was on previous step
-ar 44100 – audio rate 44100 or sometimes 48000 are most normal ones, just use what was in the output from previous step
-ab 320k – the wav file said it was 2116 kb/s which is most of the reason for the large file size of a wav file. 320k is DJ quality you will hear in a club, if you are going to listen on a mobile phone then 128k is perfectly acceptable and will save you a lot of space
-f mp3 – simply says that your output file will be in mp3 format

The output –

ffmpeg version 2.0.1 Copyright (c) 2000-2013 the FFmpeg developers
built on Sep 24 2013 05:31:18 with gcc 4.8 (Debian 4.8.1-10)
configuration: --extra-cflags=-I../static/include --extra-ldflags='-L../static/lib -static' --enable-gpl --enable-version3 --enable-static --disable-shared --disable-debug --enable-runtime-cpudetect --enable-libmp3lame --enable-libx264 --enable-libspeex --enable-libvorbis --enable-libvpx --enable-libfreetype --enable-libxvid --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-gray --enable-libopenjpeg --disable-ffserver
libavutil 52. 38.100 / 52. 38.100
libavcodec 55. 18.102 / 55. 18.102
libavformat 55. 12.100 / 55. 12.100
libavdevice 55. 3.100 / 55. 3.100
libavfilter 3. 79.101 / 3. 79.101
libswscale 2. 3.100 / 2. 3.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 3.100 / 52. 3.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, wav, from 'Abstract Illusion - Isolation VIP.wav':
Duration: 00:06:14.81, bitrate: 2116 kb/s
Stream #0:0: Audio: pcm_s24le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s32, 2116 kb/s
Output #0, mp3, to 'Abstract Illusion - Isolation VIP.mp3':
Metadata:
TSSE : Lavf55.12.100
Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, s32p, 320 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s24le -> libmp3lame)
Press [q] to stop, [?] for help
size= 14644kB time=00:06:14.83 bitrate= 320.0kbits/s
video:0kB audio:14643kB subtitle:0 global headers:0kB muxing overhead 0.007263%

You can see from the output above the input and output channels. This conversion was fast as the track is just over 6mins length (highlighted above), but it works whatever size of file you are using, just will take a bit longer!

5) Check your output file like this –

ffmpeg -i "Abstract Illusion - Isolation VIP.mp3"

ffmpeg version 2.0.1 Copyright (c) 2000-2013 the FFmpeg developers
built on Sep 24 2013 05:31:18 with gcc 4.8 (Debian 4.8.1-10)
configuration: --extra-cflags=-I../static/include --extra-ldflags='-L../static/lib -static' --enable-gpl --enable-version3 --enable-static --disable-shared --disable-debug --enable-runtime-cpudetect --enable-libmp3lame --enable-libx264 --enable-libspeex --enable-libvorbis --enable-libvpx --enable-libfreetype --enable-libxvid --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-gray --enable-libopenjpeg --disable-ffserver
libavutil 52. 38.100 / 52. 38.100
libavcodec 55. 18.102 / 55. 18.102
libavformat 55. 12.100 / 55. 12.100
libavdevice 55. 3.100 / 55. 3.100
libavfilter 3. 79.101 / 3. 79.101
libswscale 2. 3.100 / 2. 3.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 3.100 / 52. 3.100
Input #0, mp3, from 'Abstract Illusion - Isolation VIP.mp3':
Metadata:
encoder : Lavf55.12.100
Duration: 00:06:14.86, start: 0.000000, bitrate: 320 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
At least one output file must be specified

You can see that your newly converted file is now 320kb/s

And thats it. you now have an mp3 file converted from a wav that you can use on your devices. And while you are listening to your newly converted Abstract Illusion track, check out his other tracks on Soundcloud HERE

In future posts I will show you some of the other things you can do with ffmpeg with both audio and video so subscribe to the bashworkz.com website to be notified of new hints and tips I post up here.

Linux Date Problem – 1 month ago date bug

Linux Date Problem - 1 month ago date bug

Linux Date Problem – 1 month ago date bug

Linux Date Problem – 1 month ago date bug

There exists what I call the 1 month ago date bug in the GNU date command which (i believe) is on almost every unix distribution that System Admins should at least be aware of.

The date command is frequently used in bash scripts, and in cron scheduled tasks, to calculate dates in the past or future, to reformat dates into different country standards, or using its many features including seeing and using the Epoch date (number of seconds since 1st January 1970).

This post is going to show you a bug for a fairly common use of the date command which is to find the previous month. This sort of thing is useful for month end reporting, or history charts maybe.

So here is the command you would use –

date -d '1 month ago' +%Y-%m

In that example it will show me the CCYY (2016 as example), and MM (today is 1st april, therefore will show 03 for March)

All perfectly normal so far… until you run it on the 31st March (as I did last night)

-bash-4.1$ date
Thu Mar 31 14:34:23 EDT 2016
-bash-4.1$ date -d '1 month ago' +%Y-%m
2016-03
-bash-4.1$

So you see your ‘1 month ago’ is still showing the same month (March), when you expected to see February.

A little bit more investigation instead of just displaying CCYY-MM and showing the whole date it shows ‘1 month ago’ as being 2nd March. That is 29 days ago, suggesting it knows the previous month was February, it also knows in 2016 that February had 29 days in it (leap year), and took current date – 29 to get 1 month ago. Now therefore, you can see the 1 month ago date BUG !!!

So how to get around it?

The Solution

Well for my purposes I simply wanted the month, therefore an easy solution is this –

date -d "$(date +%Y-%m-15) -1 month" +%Y-%m

will display me 2016-02.

What this does is to take the current month, use a static date for that current month to be the 15th (therefore avoiding varying number of days in a month), and then looking for the previous month.

I would like to thank Paul Rieber for this, as he encountered the same issue previously and answered my question on Quora (I think) with the clearest answer, and neatest solution to something that is not just related to Centos but many distributions of Linux.

BE AWARE OF THE '1 month ago' DATE BUG

So this is just to make sure every System Admin is at least aware of the bug before it bites them.

Share this post with all your System Admin friends !!

How to solve WordPress login hack attempts

How to solve WordPress login hack attempts

How to solve WordPress login hack attempts

How to solve WordPress login hack attempts

Today I will show you how one way how to stop wordpress login hack attempts without using a plugin.

This is my second post about WordPress ive decided to publish (you can see the first one here). Is not strictly about wordpress though and can be adapted for a variety of situations where you want to stop multiple unwanted requests to your web server.

If you are like me and do not want to add another plugin to bloat your wordpress installation even more then this is one method you can use to block (or redirect) persistent ips from trying to log in.

Firstly you obviously know that this is happening probably from your apache or nginx access logs. I will focus on apache for this tutorial. In the access logs you will see something like this –

1.2.3.4 date time "POST /wp-login.php HTTP/1.1" 200 3813 etc etc

In the example above the 1.2.3.4 is the ip address of the user attempting to login. So you now want to get a list of ALL ip addresses doing it –

grep "POST /wp-login.php" | awk '{print $1}' | sort | uniq

the awk prints the first column which contains the ip address. sort and uniq is a simple way to remove duplicate ip addresses from your output.

the .htaccess file is the key to blocking these ip addresses, or what I have decided to do on one of my domains is to redirect them to a paying url through shorte.st which might earn me a few $ who knows. To add the redirect into .htaccess you need this –

For each line you then need to generate something like this for each record –

RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4 [OR]
RewriteCond %{REMOTE_ADDR} ^2\.3\.4\.5 [OR]
RewriteCond %{REMOTE_ADDR} ^3\.4\.5\.6

Notice the last one does not have the OR on the end.

Heres a quick hack script based on the previous grep to make the total list (in BOLD I have added an exclusion grep for you to add your own ip address and anyone else you DO want to let have access) –

grep "POST /wp-login.php" | awk '{print $1}' | sort | uniq | grep -v -E "ENTER YOUR IP ADDRESS AND ANY OTHERS YOU WANT TO EXCLUDE FROM THIS RULE | SEPERATED" | while read LINE ; do
echo "RewriteCond %{REMOTE_ADDR} ^${LINE} [OR]"
done | sed "s/\./\\\./g"

Now you can edit your website .htaccess and paste in the rows the script above has produced (remember to ignore the OR on the last record) –

RewriteEngine on
--enter all your records here
RewriteRule wp-login\.php http://sh.st/TI9m1 [R,L]

for testing it is probably a good idea to add your own IP address in the .htaccess file so you can prove it works as you want, and then remove it after you are happy.

This is just 1 solution out of probably many, and may not suit everyone, but hope it helps someone !

Have fun blocking those hackers (and maybe making some cash when they go through your redirect link as well) !!