Sunday, November 24, 2013

Quick ping-scanning a network using bash

So you want to know who answers to pings in said network or IP range?
Certainly not the best way to map a network, but it's useful enough to find a forgotten IP number without having to check with your DHCP:

$ for ip in 10.0.0.{000..255};do ping -c1 -t1 $ip > /dev/null 2>&1 && echo "${ip} is up" & done|grep up|sort

This method can also be used to run multiple commands in multiple terminal windows by using gnome-terminal -e "your command here".

Tuesday, October 8, 2013

Non interactive ftp for Linux users

FTP. Yes, people are still using it.

  1.  non interactive ftp login:
    $ ~/.netrc
    machine server_name/ip
    login username
    password password


    $ chmod 600 ~/.netrc
    Now you can simply ftp to said machine and it will auto-login.

  2.  Copying a file to the ftp server:
    • echo put FILE.ext | ftp server_name
      or
    •  curl -T FILE.ext ftp://server_name --user username:password

Wednesday, October 2, 2013

Checking your NIC is connected using bash

A simple yet effective way to test your network adapters connection status using  command line.

Requires: ethtool


# ifconfig -a|grep eth|while read line;do echo ${line%% *} && ethtool ${line%% *}|grep Link;done

Thursday, July 18, 2013

Slow ssh in RHEL/CentOS

It's everywhere and so it's also here, if ssh to Red Hat or CentOS takes forever, here's a quick fix:
# sed -i 's/^#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
# sed -i 's/^#GSSAPIAuthentication no/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
# sed -i 's/^GSSAPIAuthentication yes/#GSSAPIAuthentication yes/g' /etc/ssh/sshd_config
# service sshd restart

Monday, May 6, 2013

BASH CLI turn monitor off / blank screen

So you want to get your screen blank or get your monitor into standby mode from the CLI?
Add the following to your .bashrc:


alias blank='sleep 1 && xset dpms force standby'

From there on, just use the command blank from your terminal.

Saturday, April 6, 2013

Quickly add rules to IPTABLES on CentOS/RHEL 5.x using a script

Older iptables lack the -C switch so you have to use bash to check whether a rule already exists before appending it. Works like this:

if [ ! "`iptables-save | grep 'dport 21'`" ]; then iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT;fi
if [ ! "`iptables-save | grep 'dport 22'`" ]; then iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT;fi
if [ ! "`iptables-save | grep 'dport 80'`" ]; then iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT;fi
service iptables save
Probably not the most elegant solution, but it works. 

Quickly disable SELinux in a setup script

So we all know how to disable SELinux manually, but what about doing so as part of a setup script?
Easy, got to love sed!

Setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux && cat /etc/sysconfig/selinux
Repeat on /etc/selinux/config for Centos 7

Wednesday, February 20, 2013

Wednesday, February 6, 2013

phpMyAdmin - Error

Don't you just love them errors? They make you stress your mind, check everything all over again, force to communicate with people around you and basically waste your time.

One such lovely error is:

Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.
There are quite a few answers online for fixing this problem but sometimes, especially if there's a chance you did something silly along the way, it just won't help.  So, yet another solution if everything else fails:

# chmod 0777 /var/lib/php/session

Monday, February 4, 2013

Apache cluster active server

So you have Apache running in a cluster and you want to know which server is active right now. PHP can help you there.

Put this line of code in whoami.php

<?php system('hostname', $retval); ?>

Wednesday, January 16, 2013

Exiftool, quick and easy for RHEL/CentOS 5.x

Almost the latest version as of now, the one most 'production ready'.
Be root, cut and paste into your terminal and voila!

mkdir extra_packages
cd extra_packages/
wget http://owl.phy.queensu.ca/~phil/exiftool/Image-ExifTool-9.04.tar.gz
tar xvzf Image-ExifTool-9.04.tar.gz
cd Image-ExifTool-9.04
perl Makefile.PL
make
make test
make install

Tuesday, January 15, 2013

Wordpress & Varnish



Basic configuration, tiny bit of security and avoiding error 405 on post. This server runs other things as well, so configurations are not as aggressive as they might be in other places.

backend default {
.host = "127.0.0.1";
.port = "8080";
}

acl purge {
        "localhost";
}

sub vcl_recv {

    /* Before anything else we need to fix gzip compression */
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|mp4)$") {
            # No point in compressing these
            remove req.http.Accept-Encoding;
        } else if (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } else if (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            # unknown algorithm
            remove req.http.Accept-Encoding;
        }
    }
    # Login, administration and posting comments are to be completely and utterly left alone, no caching, no nothing.
    if (req.http.host == "my.blog.com") {
        if (req.url ~ "wp-(login|admin|comments-post)") {
                set req.backend = default;
            return(pipe);
        }
        # Drop any cookies sent to Wordpress.
    unset req.http.cookie;
        if (req.request == "PURGE") {
                if (!client.ip ~ purge) {
                        error 405 "Not allowed.";
                }
        }
        set req.backend = default;
        return (lookup);
    }
     if (req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|gz|zip|rar|bz2|tgz|tbz|html|htm|pdf|mp4)$") {
         unset req.http.cookie;
     set req.grace = 5m;
         return(lookup);
     }
     return(pass);
}

sub vcl_fetch {
    if (beresp.status == 302) {
        set beresp.http.X-Magic-Redirect = "1";
        return(deliver);
    }
    if (req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|gz|zip|rar|bz2|tgz|tbz|html|htm|pdf|mp4)$") {
        unset beresp.http.set-cookie;
    }
    set beresp.do_stream = true;
    if (req.http.Authorization) {
        return (hit_for_pass);
    }
}

sub vcl_hit {
    if (obj.http.X-Magic-Redirect == "1") {
        set req.url = obj.http.Location;
        return (restart);
    }

    if (req.request == "PURGE") {
        set obj.ttl = 0s;
        error 200 "Purged.";
    }

}
sub vcl_deliver {
    if (resp.http.X-Magic-Redirect == "1") {
        unset resp.http.X-Magic-Redirect;
        return(restart);
    }
    return(deliver);
}

Monday, January 7, 2013

Building FFMPEG for Centos


For some reason, FFMPEG on RHEL/Centos is pretty much crap for just about anything I needed so far, so the only option was building it myself. Hardly as simple as it might seem, most recipes out there are... crap. Yes, they are, really, especially when dealing with people needing it for video streaming with watermarks etc. etc. etc.

Anyway, this recipe works:

https://ffmpeg.org/trac/ffmpeg/wiki/CentosCompilationGuide

Good luck, you might need it.

UPDATE: the link above does no longer work for CentOS 5.9, at least not without some serious voodoo.
So, a cut and paste recipe to turn your relatively old linux into a media churning and streaming machine (executed as root):

rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
yum install zlib-devel autoconf automake gcc-c++  SDL-devel.x86_64 libexif.x86_64 libmcrypt.x86_64 libtool git perl-Git nasm freeglut-devel.x86_64 freetype-devel.x86_64 -y

echo "1   ImageMagic"
mkdir ~/extra_packages/
cd ~/extra_packages/
wget ftp://ftp.sunet.se/pub/multimedia/graphics/ImageMagick/linux/CentOS/x86_64/ImageMagick-6.7.9-10.x86_64.rpm
wget ftp://ftp.sunet.se/pub/multimedia/graphics/ImageMagick/linux/CentOS/x86_64/ImageMagick-devel-6.7.9-10.x86_64.rpm
yum localinstall ImageMagick-6.7.9-10.x86_64.rpm ImageMagick-devel-6.7.9-10.x86_64.rpm -y --nogpgcheck
cd -

echo "2   FFmpeg transcoder"
echo "(Based on http://ffmpeg.org/trac/ffmpeg/wiki/CentosCompilationGuide)"

mkdir ~/ffmpeg-source

echo "Yasm"
echo "Yasm is an assembler used by x264 and FFmpeg."
cd ~/ffmpeg-source
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xzvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make
make install
cd -

echo "x264"
echo "H.264 video encoder."
cd ~/ffmpeg-source
git clone git://git.videolan.org/x264
cd x264
./configure --enable-static
make
make install
cd -

echo "LAME"
echo "MP3 audio encoder."
cd ~/ffmpeg-source
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --disable-shared --enable-nasm
make
make install
cd -

echo "libvpx"
echo "VP8 video encoder."
cd ~/ffmpeg-source
git clone http://git.chromium.org/webm/libvpx.git
cd libvpx
./configure
make
make install
cd -

echo "vo-aacenc"
echo "VisualOn AAC audio encoder."
cd ~/ffmpeg-source
wget http://downloads.sourceforge.net/opencore-amr/vo-aacenc-0.1.2.tar.gz
tar xzvf vo-aacenc-0.1.2.tar.gz
cd vo-aacenc-0.1.2
./configure --disable-shared
make
make install
cd -

echo "FFmpeg"
cd ~/ffmpeg-source
git clone git://source.ffmpeg.org/ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-libmp3lame  --enable-libvo-aacenc --enable-libvpx --enable-libx264 --enable-version3
make
make install
ln -s /usr/local/bin/ffmpeg /bin/ffmpeg
cd -

echo "3   Exiftool"
mkdir ~/extra_packages
cd ~/extra_packages/
wget http://owl.phy.queensu.ca/~phil/exiftool/Image-ExifTool-9.04.tar.gz
tar xvzf Image-ExifTool-9.04.tar.gz
cd Image-ExifTool-9.04
perl Makefile.PL
make
make test
make install
cd -

echo "4 MP4Box"
lastdir=`pwd`
mkdir ~/MP4box
cd !$
wget http://sourceforge.net/projects/gpac/files/GPAC/GPAC%200.4.5/gpac-0.4.5.tar.gz/download
wget http://sourceforge.net/projects/gpac/files/GPAC%20extra%20libs/GPAC%20extra%20libs%200.4.5/gpac_extra_libs-0.4.5.tar.gz/download
tar -xzf gpac-0.4.5.tar.gz
tar -xzf gpac_extra_libs-0.4.5.tar.gz
cd gpac_extra_libs
cp -rf * ../gpac/extra_lib/
cd ../gpac
chmod +x configure
./configure
make lib
make apps
make install lib
make install
cp bin/gcc/libgpac.so /usr/lib/
ln -s /usr/local/bin/MP4Box /bin/MP4Box
ldconfig
cd $lastdir