Tuesday, July 26, 2016

[Quick Tips: Bash File] Reading contents of file through Scripts

Reading contents of file through Scripts



the same file content as:
This is a test.
Unix is Best.
No Linux is the Best.
Space in simple understanding is an area or volume.
Outer space .

I need the output:

Unix is Best.
Outer space .


How do I print all lines that have three (3) words only?

The awk command is well suitable for this kind of pattern processing text file. Awk set the variable called NF. It is set to the total number of fields in the input record. So if NF equal to three print the line. The syntax is as follows:
awk '{ if ( NF == 3 ) print } ' /path/to/input
It is also possible to emulate awk command output using a shell script while loop and IFS (internal field separator) in loops:
#!/bin/bash
# AWK NF if condition (awk '{ if ( NF == 3 ) print } ' $_input) emulation using bash 
# Author: nixCraft <www.cyberciti.biz>
# -----------------------------------------------------------------------------------
_input="/path/to/data.txt"
_word=3
while IFS= read -r line
do
 set -- $line
 [ $# -eq $_word ] &&  echo "$line"
done < "$_input"
Sample outputs:
Unix is Best.
Outer space .

[Quick Tips: Bash Script]: A Tutorial on Bash Script

A Tutorial on Bash Script

Use bash For Loop In One Line

Run Command 5 Times

for i in {1..5}; do COMMAND-HERE; done
OR
 for((i=1;i<=10;i+=2)); do echo "Welcome $i times"; done

Work On Files

for i in *; do echo $i; done
OR
for i in /etc/*.conf; do cp $i /backup; done

For Loop To Remove Files

Use a Bash For Loop To Delete Files in Unix

for f in /path/to/dir/*.txt
do
   # if file, delete it
   [ -f "$f" ] && rm "$f"
done
See Bash for loop tutorial for more information.

Use a KSH For Loop To Delete Files in Unix

for f in /path/to/dir/*.c~; do
        # if it is a file, delete it
 if [  -f $f ]
 then
   rm "$f"
 fi
done

For While Loop Through File Contents Script

while COMMANDS do COMMANDS done OR
while read line do echo "$line" done < /path/to/input.txt
In this example, read /etc/passwd file line by line using while loop:
#!/bin/bash
while IFS= read -r line
do
  echo "$line"
done < "/etc/passwd"
Another example:
#!/bin/bash
## script.sh ##
_file="${1:-/dev/null}"   #fail safe 
while IFS= read -r line
do
  # Do something on $line
  echo "$line"
done < "$_file"
Run it as follows: chmod +x script.sh ./script.sh /path/to/text/file.name

Monday, July 18, 2016

[Quick info: Kill Sessions]: how do I kill another login session remotely?

How do I kill another login session remotely?

You can kill a Unix login session remotely by sending a hangup signal (SIGHUP) to the process running the login session. To do this, follow the steps below:
  1. Identify the shell you want to kill. To determine your current tty, from your Unix shell prompt, enter:
      tty
  2. To show all of your running processes, enter:
      ps -fu username
    Replace username with your username.
  3. You should see something like this:
      PID    TT  STAT   TIME COMMAND
      13964  v5   I      0:00 elm
      13126  ue   S      0:00 -bash (bash)
      13133  ue   R      0:00 ps x
      13335  v5   S      0:00 -bash (bash)
    In the first column, "PID" stands for "process ID". The second column shows the tty to which your processes are connected. The dash (-) before a process name shows that the process is a login shell.
  4. To remove the remote shell, look for the processes with a dash and choose the process number that is not for your current tty. Then issue the following command:
      kill -HUP processid
    Replace processid with the process ID number you identified.
When you send a SIGHUP (by entering kill -HUP or kill -1) to a login shell, all the processes that were started in the shell will be killed as well (unless they were in the background). SIGHUP is good because it allows applications like Elm and Emacs to exit gracefully, leaving your files intact.
Note: You cannot kill processes that are running on a computer different from the one you are logged into. This rule extends to individual nodes within clusters of Unix systems as well.
++++++++=======================================+++++++++++++++++++

Howto: Linux Kill and Logout Users

There is a package called procps. It includes various useful (nifty) utilities. One of such utility is skill which is responsible to send a signal to users and process such as:
  • Halt user terminal
  • Kill user and logout

The procps package contains utilities to browse the /proc filesystem, which is not a real file system but a way for the kernel to provide information about the status of entries in its process table. Procps includes ps, free, skill, pkill, pgrep, snice, tload, top, uptime, vmstat, w, watch and pdwx commands.

Task: How To Halt/Stop a User Called vivek

Open a command-line terminal (select Applications > Accessories > Terminal), and then type the following commands. First, switch to the root user by typing su – and entering the root password, when prompted (you can also use sudo if configured). Type the skill command as follows:
# skill -STOP -u vivek
The skill command sends a terminate command (or another specified signal) to a specified set of processes.

Task: Resume Halted User Called vivek

Send CONT single to user vivek, type the following command:
# skill -CONT -u vivek

Task: Kill and Logout a User Called vivek

You can send KILL single, type the following command:
# skill -KILL -u vivek

Task: Kill and Logout All Users

The ultimate command to kill and logout all users is as follows:
# skill -KILL -v /dev/pts/*
WARNING! These tools are obsolete, unportable and it is here due to historical reasons. Consider using the killall, pkill, and pgrep commands instead as follows.

pkill command

To halt or stop a user called vivek, enter:
# pkill -STOP -u vivek
To resume a user called vivek, enter:
# pkill -CONT -u vivek
To kill all php-cgi process owned by vivek user, enter:
# pkill -KILL -u vivek php-cgi

Other useful nifty utilities provided by procps package

  • w command : Show who is logged on and what they are doing.
  • kill command : Send signal to a process (explains how to kill process under Linux)
  • top command : Display Linux tasks and other important stuff
  • vmstat command : Display virtual memory statistics.
  • free command : Display free and used memory (RAM) statistics.
  • slabtop command : Display kernel slab cache information in real time.

[Quick Install: NAS-XPEnology]: XPEnology - NAS Box setup

XPEnology - NAS Box setup

XPEnology.me

Download from : http://xpenology.me/downloads/

DSM

The heart of the device is its Synology operating system DSM (Disk Station Manager), which is used in all devices produced by Synology. It is a well-optimized Linux kernel, most of the changes which aims to work with hard disk drives (full list of compatible discs) and Raid arrays. As well it is composed of many Open source packages, details of which you will find just the manufacturer’s website. Let respects the company’s Sinology! They did a great job – their products are the best and we will tell why. Thanks to them!
DSM loved by many, but unfortunately Sinology not released a version for PC, the operating system is not compatible with the hardware of many PCs and servers are not produced by Synology.

WARNING

Despite the fact that the development team was able to release the bootloader, allowing DSM run on almost any computer, we strongly recommend that you refrain from using a bootloader on production and buy it from the above-mentioned original products from Synology.
Use it at your own risk! we will not be held responsible for any data loss or broken machine.

What is XPEnology

DSM is loaded on a synology Device with USB flash drive, soldered on the motherboard, the operation system and all applications located are in special slice of the hard disk drives (on each) this method allows to increase the service life of the boot flash and increase productivity.
XPEnology is a bootloader as IMG image. XPEnology compiled from the source code provided Sinology under license GNUXPEnology and does not contain the operating system.
In addition to the kernel in the boot loader included some scripts and driver packages to work on most particular hardware platforms.
XPEnoboot is published by a team of contributors:
  • Vortex
  • Sancome
  • Kali
  • Jman
  • Poechi
XPEnoboot is similar with Nanoboot, but added more drivers and fix some problems, the most important is that it support DSM5.1 installation (special thanks to Vortex).
You can download it from Sancome website nanoboot.eu.org, or this website. Also Poechi website xpenology.nl

[CentOS: gFTP]: How To Use Gftp In CentOS?

How To Use Gftp In CentOS?


Read details from the below url:
http://www.gftp.org/faq.html


to Download it from:
http://pkgs.repoforge.org/gftp/

1.1. What are the requirements to run gFTP?



  • GTK+ 1.2.3 or higher. GTK+/Glib is included with most systems now. If you don't have it installed, you may want to check to see if your vendor has a package available. If not, you can download the latest version of GTK+ and Glib from ftp://ftp.gtk.org/pub/gtk. gFTP will work with either version 1.2 or 2.x of GTK+, but I would recommend using GTK+ 2.x.
  • pthread libraries. Most systems already satisfy this requirement. If you are on a platform other than Linux, you may want to take a look at the FSU Pthreads library at http://www.informatik.hu-berlin.de/~mueller/pthreads.

1.2. How do I install gFTP?



  • If you do not want to compile gFTP yourself, you can check my webpage. I have DEBs for Debian and RPMs for RedHat available. If you're looking for Solaris binaries, check out http://www.sunfreeware.com.
  • If you are compiling gFTP yourself, extract the files from the distribution with tar -zxvf gftp-<version>.tar.gz
  • Go to the gftp-<version> directory and type
    ./configure
    make
    The configure script will generate a Makefile for your machine, and make will compile a binary for your machine. By default, when you run configure, it will install gFTP with the prefix /usr/local, so the gFTP binary will be /usr/local/bin/gftp. You can change this by typing configure --prefix=/usr, and it will then install the gFTP binary to /usr/bin/gftp.
  • If the compile suceeds without any errors, you can type make install to install the binary. This will also copy some necessary files to <install prefix>/share/gftp. If you do not have root on your machine to install these files, copy the contents of the docs/sample.gftp/ directory to ~/.gftp.
  • You may want to glance at the USERS-GUIDE file that comes with this distribution. It contains a little bit of information about how to use it.

1.3. What systems is gFTP known to run on?

I develop gFTP on a Debian GNU/Linux machine. It should run under any other Linux distributions. Here is a list of other platforms I have received successful compile reports:

  • FreeBSD/NetBSD/OpenBSD/BSDI
  • Solaris
  • IRIX
  • Digital UNIX
  • HP/UX 11
If gFTP compiles and runs on a platform not listed here, please email me about it and I'll add it to this list. If you are having problems compiling GTK+ or gFTP on another platform, try using GNU make instead of your vendor supplied make.

1.4. Does gFTP have a SVN repository?

gFTP is stored in GNOME's SVN repository. For instructions on how to setup your subversion client, visit the website http://developer.gnome.org/tools/svn.html. In a nutshell, you will need to run the following command to download the latest code:

svn co http://svn.gnome.org/svn/gftp/trunk gftp

To compile the SVN code, you will need autoconfautomake and gettext installed on your system. Once you have those packages installed, run the following series of commands:

  • Run ./autogen.sh to generate the configure scripts.
  • If that is successful, run ./configure to generate the Makefiles. By default, the binaries will be installed in /usr/local/bin. You can change the install location by passing the --prefix argument to configure. For example, let's say you want to install the binaries in /usr/bin. Run configure --prefix=/usr.
  • If that is successful, run make to build the source.
  • Optional: If that is successful, run make install to install the binary on your computer.

1.5. How do I report bugs in gFTP?

If you find ANY bugs in gFTP, please check to make sure that you are using the latest version of gFTP. If you are, please submit all bug reports to GNOME's Bugzilla. When sending in bug reports, please try to be as descriptive as possible. Send me what OS/version you are running, what compiler you are compiling with, the output of gftp --info and any other important information. If you get gFTP to crash, it would be very helpful if you could send me a backtrace. If you do not know how to do a backtrace, download the source code from my website. Edit the Makefile, and make sure that -g appears in the CFLAGS variable. Then recompile gFTP, but do NOT run make install because it will install a stripped binary in $PREFIX/bin. Then type gdb ./gftp. Once you have a (gdb) prompt, type r. When gFTP crashes, go back to that terminal window, and type bt, and put that output in the bug that you file with bugzilla.
I also have a gftp-users mailinglist setup. I have a link to the archives on my main webpage. If it is strictly a bug report, please submit it to Bugzilla instead of the mailinglist. Bugs that are submitted to bugzilla are much easier for me to track.
Also, if you do not get a response back from me immediately, that is not uncommon. I usually stay very busy and I don't get to work on gFTP all of the time.

1.6. How do I force running the text or gtk+ version of gFTP?

To run the text port, you can type gftp-text or to run the gtk+ port, you can run the gftp-gtk. The gftp command is just a shell script that checks if your DISPLAY variable is set, and if so it'll run the appropriate version.

1.7. Is there a way for gFTP to download a bunch of files/directories and then exit when it's completed?

Yes, the text port of gFTP supports this well. You can type:

gftp-text -d ftp://ftp.somesite.com/someplace

If someplace is a directory, it'll automatically download all of its subdirectories as well. If you want to transfer a file through ssh instead of ftp, just change the ftp:// to ssh://.

1.8. In the GTK+ version of gFTP, do I have to enter a port, username and password to connect to a ftp server?

No you don't. If you leave the port blank, it'll default to the default port for the protocol you selected (port 21 for FTP). If you leave the username blank, it will default to logging in as anonymous.

1.9. Where does gFTP store it's options?

gFTP will automatically create a ~/.gftp directory when it is first run. Your config file is ~/.gftp/gftprc, and this is where all of gFTP's settings are stored. The config file is well commented, so take a glance at it and see if there is anything you want to change. Your bookmarks are stored in the file ~/.gftp/bookmarks.
Every time gFTP is run, it will log the contents of the log window to ~/.gftp/gftp.log. The contents of this file will be automatically purged this file when gFTP is started up.

1.10. Why aren't the file sizes comma separated?

If gFTP was compiled with _GNU_SOURCE defined (run gftp --info), then gFTP will use the printf() modifier %'ld. If your LANG environment is set to C, then no commas will be inserted. If you are in the US, change your LANG environment variable to en_US.

1.11. When using an external editor, gFTP does not detect changes that are being made to the file.

Look at the man page for the editor you are using and see if there is a command line argument to have the editor run in the foreground (it won't fork to detact from the current terminal). If you are using one of the graphical VIM editors, you can add the --nofork argument.

1.12. How can I increase the size of the fonts in gFTP?

You can use the gnome-font-properties program to change the fonts used by all GTK+ 2.x applications.

1.13. I can't transfer certain file types in binary mode using the FTP protocol.

Edit your ~/.gftp/gftprc file and look at the ext= lines towards the bottom of the file. These lines control what icon is used for each file type. It also controls what mode is used for the file transfer. For example, to transfer all HTML files as binary, change the following two lines:

ext=.htm:world.xpm:A:
ext=.html:world.xpm:A:

to the following:

ext=.htm:world.xpm:B:
ext=.html:world.xpm:B:

Chapter 2. FTP Protocol

2.1. When gFTP tries to get the remote directory listing, the remote server complains about either the -a or -L argument to LIST.

If the server is complaining about the -L option, go under FTP->Options->FTP and disable resolve remote symlinks. If it is complaining about the -a option, go to FTP->Options->General and disable show hidden files.

2.2. When gFTP tries to get the remote directory listing, I receive the error: Cannot create a data connection: Connection refused

Go under FTP->Options->FTP and turn off passive file transfers. Instead of sending the PASV command to open up the data connection on the server side, the data connection will be opened up on the client side, and the PORT command will be sent to the server instead.

Chapter 3. HTTP Protocol

3.1. How do I set up my HTTP Proxy server?

Go under FTP->Options and hit the FTP tab. Enter HTTP as the proxy type and fill in your proxy server info on that screen. Then hit the HTTP tab and enter your same proxy server info there.

Chapter 4. SSH Protocol

4.1. gFTP will not log into the remote SSH server when using a non-English locale.

Note: This answer only applies to versions of gFTP >= 2.0.18
You will need to download the source code to gFTP and edit the appropriate po/<lang>.po file. You will need to make sure the following strings are translated exactly as they appear in the SSH client.

  • Enter passphrase for RSA key
  • Enter passphrase for key
  • Password
  • password
Please upload the changes to the PO file to GNOME's SVN or email me the updated PO file. If you email me the updates, please make sure that you grab the latest PO file from SVN.

4.2. I'm having problems getting SSH2 transfers to work. I keep getting the error: Error: Message size XXXXXXXX too big from server

Note: This answer only applies to versions of gFTP <= 2.0.17
What probably is the problem, sftp-server isn't in your path on the remote server. When you try to connect to the remote server, you'll see the command gFTP is trying to run, and it'll look something like:

ssh -e none -l username hostname "echo -n xsftp ; sftp-server"

Try running this command. If you get a prompt asking:

The authenticity of host 'hostname (###.###.###.###)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?

Tell it yes. gFTP won't even try to answer this question for you at the moment. Then, if you get the following output:

Enter passphrase for key '/home/brian/.ssh/id_dsa':
xsftp
bash: sftp-server: command not found

You'll have to set the path to the sftp-server program. Alternatively you could go under FTP->Options->SSH and enable Use sftp subsys. If you enable this, then you don't have to know the remote path to your sftp executable. But if you enable this, gFTP won't be able to send your password over to the ssh client. So if you're not using ssh-agent, then ssh will spawn the ssh-askpass program to grab your password.

4.3. What is the difference between the SSH and SSH2 protocols?

Note: This answer only applies to versions of gFTP <= 2.0.15
The SSH protocol uses sftp >= 0.7 from http://www.xbill.org/sftp. SSH2 uses the native file transfers found in the commercial SSH2 and in later versions of OpenSSH. The SSH2 protocol uses the sftp-server executable on the remote host and the SSH protocol uses the sftpserv executable.
Note: Due to confusion, the origional SSH protocol was removed in gFTP 2.0.15. It is not shown in gFTP 2.0.14, but there is a config option called enable_old_ssh to enable this protocol.

Chapter 5. SSL Issues

5.1. When using the FTPS or HTTPS protocol, gFTP cannot connect if the remote server uses a self signed certificate.

You must add the public key of your self signed CA to your OpenSSL certs directory. On my Debian box, the OpenSSL certs are installed in /usr/lib/ssl/certs.

Chapter 6. Compiling Problems

6.1. When I try to compile gFTP, I see undefined symbol libintl_gettext. Or, another error is undefined symbol LC_ALL.

Try running

make distclean ; configure --with-included-gettext.

You could alternatively pass
--disable-nls
to configure, and internationalization support will not be compiled in.


[CentOS: gFTP]: How To Use Gftp In CentOS?

How To Use Gftp In CentOS?


Read details from the below url:
http://www.gftp.org/faq.html


to Download it from:
http://pkgs.repoforge.org/gftp/

1.1. What are the requirements to run gFTP?



  • GTK+ 1.2.3 or higher. GTK+/Glib is included with most systems now. If you don't have it installed, you may want to check to see if your vendor has a package available. If not, you can download the latest version of GTK+ and Glib from ftp://ftp.gtk.org/pub/gtk. gFTP will work with either version 1.2 or 2.x of GTK+, but I would recommend using GTK+ 2.x.
  • pthread libraries. Most systems already satisfy this requirement. If you are on a platform other than Linux, you may want to take a look at the FSU Pthreads library at http://www.informatik.hu-berlin.de/~mueller/pthreads.

1.2. How do I install gFTP?



  • If you do not want to compile gFTP yourself, you can check my webpage. I have DEBs for Debian and RPMs for RedHat available. If you're looking for Solaris binaries, check out http://www.sunfreeware.com.
  • If you are compiling gFTP yourself, extract the files from the distribution with tar -zxvf gftp-<version>.tar.gz
  • Go to the gftp-<version> directory and type
    ./configure
    make
    The configure script will generate a Makefile for your machine, and make will compile a binary for your machine. By default, when you run configure, it will install gFTP with the prefix /usr/local, so the gFTP binary will be /usr/local/bin/gftp. You can change this by typing configure --prefix=/usr, and it will then install the gFTP binary to /usr/bin/gftp.
  • If the compile suceeds without any errors, you can type make install to install the binary. This will also copy some necessary files to <install prefix>/share/gftp. If you do not have root on your machine to install these files, copy the contents of the docs/sample.gftp/ directory to ~/.gftp.
  • You may want to glance at the USERS-GUIDE file that comes with this distribution. It contains a little bit of information about how to use it.

1.3. What systems is gFTP known to run on?

I develop gFTP on a Debian GNU/Linux machine. It should run under any other Linux distributions. Here is a list of other platforms I have received successful compile reports:

  • FreeBSD/NetBSD/OpenBSD/BSDI
  • Solaris
  • IRIX
  • Digital UNIX
  • HP/UX 11
If gFTP compiles and runs on a platform not listed here, please email me about it and I'll add it to this list. If you are having problems compiling GTK+ or gFTP on another platform, try using GNU make instead of your vendor supplied make.

1.4. Does gFTP have a SVN repository?

gFTP is stored in GNOME's SVN repository. For instructions on how to setup your subversion client, visit the website http://developer.gnome.org/tools/svn.html. In a nutshell, you will need to run the following command to download the latest code:

svn co http://svn.gnome.org/svn/gftp/trunk gftp

To compile the SVN code, you will need autoconfautomake and gettext installed on your system. Once you have those packages installed, run the following series of commands:

  • Run ./autogen.sh to generate the configure scripts.
  • If that is successful, run ./configure to generate the Makefiles. By default, the binaries will be installed in /usr/local/bin. You can change the install location by passing the --prefix argument to configure. For example, let's say you want to install the binaries in /usr/bin. Run configure --prefix=/usr.
  • If that is successful, run make to build the source.
  • Optional: If that is successful, run make install to install the binary on your computer.

1.5. How do I report bugs in gFTP?

If you find ANY bugs in gFTP, please check to make sure that you are using the latest version of gFTP. If you are, please submit all bug reports to GNOME's Bugzilla. When sending in bug reports, please try to be as descriptive as possible. Send me what OS/version you are running, what compiler you are compiling with, the output of gftp --info and any other important information. If you get gFTP to crash, it would be very helpful if you could send me a backtrace. If you do not know how to do a backtrace, download the source code from my website. Edit the Makefile, and make sure that -g appears in the CFLAGS variable. Then recompile gFTP, but do NOT run make install because it will install a stripped binary in $PREFIX/bin. Then type gdb ./gftp. Once you have a (gdb) prompt, type r. When gFTP crashes, go back to that terminal window, and type bt, and put that output in the bug that you file with bugzilla.
I also have a gftp-users mailinglist setup. I have a link to the archives on my main webpage. If it is strictly a bug report, please submit it to Bugzilla instead of the mailinglist. Bugs that are submitted to bugzilla are much easier for me to track.
Also, if you do not get a response back from me immediately, that is not uncommon. I usually stay very busy and I don't get to work on gFTP all of the time.

1.6. How do I force running the text or gtk+ version of gFTP?

To run the text port, you can type gftp-text or to run the gtk+ port, you can run the gftp-gtk. The gftp command is just a shell script that checks if your DISPLAY variable is set, and if so it'll run the appropriate version.

1.7. Is there a way for gFTP to download a bunch of files/directories and then exit when it's completed?

Yes, the text port of gFTP supports this well. You can type:

gftp-text -d ftp://ftp.somesite.com/someplace

If someplace is a directory, it'll automatically download all of its subdirectories as well. If you want to transfer a file through ssh instead of ftp, just change the ftp:// to ssh://.

1.8. In the GTK+ version of gFTP, do I have to enter a port, username and password to connect to a ftp server?

No you don't. If you leave the port blank, it'll default to the default port for the protocol you selected (port 21 for FTP). If you leave the username blank, it will default to logging in as anonymous.

1.9. Where does gFTP store it's options?

gFTP will automatically create a ~/.gftp directory when it is first run. Your config file is ~/.gftp/gftprc, and this is where all of gFTP's settings are stored. The config file is well commented, so take a glance at it and see if there is anything you want to change. Your bookmarks are stored in the file ~/.gftp/bookmarks.
Every time gFTP is run, it will log the contents of the log window to ~/.gftp/gftp.log. The contents of this file will be automatically purged this file when gFTP is started up.

1.10. Why aren't the file sizes comma separated?

If gFTP was compiled with _GNU_SOURCE defined (run gftp --info), then gFTP will use the printf() modifier %'ld. If your LANG environment is set to C, then no commas will be inserted. If you are in the US, change your LANG environment variable to en_US.

1.11. When using an external editor, gFTP does not detect changes that are being made to the file.

Look at the man page for the editor you are using and see if there is a command line argument to have the editor run in the foreground (it won't fork to detact from the current terminal). If you are using one of the graphical VIM editors, you can add the --nofork argument.

1.12. How can I increase the size of the fonts in gFTP?

You can use the gnome-font-properties program to change the fonts used by all GTK+ 2.x applications.

1.13. I can't transfer certain file types in binary mode using the FTP protocol.

Edit your ~/.gftp/gftprc file and look at the ext= lines towards the bottom of the file. These lines control what icon is used for each file type. It also controls what mode is used for the file transfer. For example, to transfer all HTML files as binary, change the following two lines:

ext=.htm:world.xpm:A:
ext=.html:world.xpm:A:

to the following:

ext=.htm:world.xpm:B:
ext=.html:world.xpm:B:

Chapter 2. FTP Protocol

2.1. When gFTP tries to get the remote directory listing, the remote server complains about either the -a or -L argument to LIST.

If the server is complaining about the -L option, go under FTP->Options->FTP and disable resolve remote symlinks. If it is complaining about the -a option, go to FTP->Options->General and disable show hidden files.

2.2. When gFTP tries to get the remote directory listing, I receive the error: Cannot create a data connection: Connection refused

Go under FTP->Options->FTP and turn off passive file transfers. Instead of sending the PASV command to open up the data connection on the server side, the data connection will be opened up on the client side, and the PORT command will be sent to the server instead.

Chapter 3. HTTP Protocol

3.1. How do I set up my HTTP Proxy server?

Go under FTP->Options and hit the FTP tab. Enter HTTP as the proxy type and fill in your proxy server info on that screen. Then hit the HTTP tab and enter your same proxy server info there.

Chapter 4. SSH Protocol

4.1. gFTP will not log into the remote SSH server when using a non-English locale.

Note: This answer only applies to versions of gFTP >= 2.0.18
You will need to download the source code to gFTP and edit the appropriate po/<lang>.po file. You will need to make sure the following strings are translated exactly as they appear in the SSH client.

  • Enter passphrase for RSA key
  • Enter passphrase for key
  • Password
  • password
Please upload the changes to the PO file to GNOME's SVN or email me the updated PO file. If you email me the updates, please make sure that you grab the latest PO file from SVN.

4.2. I'm having problems getting SSH2 transfers to work. I keep getting the error: Error: Message size XXXXXXXX too big from server

Note: This answer only applies to versions of gFTP <= 2.0.17
What probably is the problem, sftp-server isn't in your path on the remote server. When you try to connect to the remote server, you'll see the command gFTP is trying to run, and it'll look something like:

ssh -e none -l username hostname "echo -n xsftp ; sftp-server"

Try running this command. If you get a prompt asking:

The authenticity of host 'hostname (###.###.###.###)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?

Tell it yes. gFTP won't even try to answer this question for you at the moment. Then, if you get the following output:

Enter passphrase for key '/home/brian/.ssh/id_dsa':
xsftp
bash: sftp-server: command not found

You'll have to set the path to the sftp-server program. Alternatively you could go under FTP->Options->SSH and enable Use sftp subsys. If you enable this, then you don't have to know the remote path to your sftp executable. But if you enable this, gFTP won't be able to send your password over to the ssh client. So if you're not using ssh-agent, then ssh will spawn the ssh-askpass program to grab your password.

4.3. What is the difference between the SSH and SSH2 protocols?

Note: This answer only applies to versions of gFTP <= 2.0.15
The SSH protocol uses sftp >= 0.7 from http://www.xbill.org/sftp. SSH2 uses the native file transfers found in the commercial SSH2 and in later versions of OpenSSH. The SSH2 protocol uses the sftp-server executable on the remote host and the SSH protocol uses the sftpserv executable.
Note: Due to confusion, the origional SSH protocol was removed in gFTP 2.0.15. It is not shown in gFTP 2.0.14, but there is a config option called enable_old_ssh to enable this protocol.

Chapter 5. SSL Issues

5.1. When using the FTPS or HTTPS protocol, gFTP cannot connect if the remote server uses a self signed certificate.

You must add the public key of your self signed CA to your OpenSSL certs directory. On my Debian box, the OpenSSL certs are installed in /usr/lib/ssl/certs.

Chapter 6. Compiling Problems

6.1. When I try to compile gFTP, I see undefined symbol libintl_gettext. Or, another error is undefined symbol LC_ALL.

Try running

make distclean ; configure --with-included-gettext.

You could alternatively pass
--disable-nls
to configure, and internationalization support will not be compiled in.


Saturday, July 2, 2016

[Quick Install: HA Proxy/ Loadbalancer]: How to configure HTTP load balancer with HAProxy on Linux

How to configure HTTP load balancer with HAProxy on Linux

 

Increased demand on web based applications and services are putting more and more weight on the shoulders of IT administrators. When faced with unexpected traffic spikes, organic traffic growth, or internal challenges such as hardware failures and urgent maintenance, your web application must remain available, no matter what. Even modern devops and continuous delivery practices can threaten the reliability and consistent performance of your web service.
Unpredictability or inconsistent performance is not something you can afford. But how can we eliminate these downsides? In most cases a proper load balancing solution will do the job. And today I will show you how to set up HTTP load balancer using HAProxy.

What is HTTP load balancing?

HTTP load balancing is a networking solution responsible for distributing incoming HTTP or HTTPS traffic among servers hosting the same application content. By balancing application requests across multiple available servers, a load balancer prevents any application server from becoming a single point of failure, thus improving overall application availability and responsiveness. It also allows you to easily scale in/out an application deployment by adding or removing extra application servers with changing workloads.

Where and when to use load balancing?

As load balancers improve server utilization and maximize availability, you should use it whenever your servers start to be under high loads. Or if you are just planning your architecture for a bigger project, it's a good habit to plan usage of load balancer upfront. It will prove itself useful in the future when you need to scale your environment.

What is HAProxy?

HAProxy is a popular open-source load balancer and proxy for TCP/HTTP servers on GNU/Linux platforms. Designed in a single-threaded event-driven architecture, HAproxy is capable of handling 10G NIC line rate easily, and is being extensively used in many production environments. Its features include automatic health checks, customizable load balancing algorithms, HTTPS/SSL support, session rate limiting, etc.

What are we going to achieve in this tutorial?

In this tutorial, we will go through the process of configuring a HAProxy-based load balancer for HTTP web servers.

Prerequisites

You will need at least one, or preferably two web servers to verify functionality of your load balancer. We assume that backend HTTP web servers are already up and running.

Install HAProxy on Linux

For most distributions, we can install HAProxy using your distribution's package manager.

Install HAProxy on Debian

In Debian we need to add backports for Wheezy. To do that, please create a new file called "backports.list" in /etc/apt/sources.list.d, with the following content:
1
deb http://cdn.debian.net/debian wheezy­backports main
Refresh your repository data and install HAProxy.
# apt­ get update
# apt ­get install haproxy

Install HAProxy on Ubuntu

# apt ­get install haproxy

Install HAProxy on CentOS and RHEL

# yum install haproxy

Configure HAProxy

In this tutorial, we assume that there are two HTTP web servers up and running with IP addresses 192.168.100.2 and 192.168.100.3. We also assume that the load balancer will be configured at a server with IP address 192.168.100.4.
To make HAProxy functional, you need to change a number of items in /etc/haproxy/haproxy.cfg. These changes are described in this section. In case some configuration differs for different GNU/Linux distributions, it will be noted in the paragraph.

1. Configure Logging

One of the first things you should do is to set up proper logging for your HAProxy, which will be useful for future debugging. Log configuration can be found in the global section of /etc/haproxy/haproxy.cfg. The following are distro-specific instructions for configuring logging for HAProxy.
CentOS or RHEL:
To enable logging on CentOS/RHEL, replace:
1
log         127.0.0.1 local2
with:
1
log         127.0.0.1 local0
The next step is to set up separate log files for HAProxy in /var/log. For that, we need to modify our current rsyslog configuration. To make the configuration simple and clear, we will create a new file called haproxy.conf in /etc/rsyslog.d/ with the following content.
1
2
3
4
5
6
$ModLoad imudp
$UDPServerRun 514 
$template Haproxy,"%msg%\n"
local0.=info ­/var/log/haproxy.log;Haproxy
local0.notice ­/var/log/haproxy­status.log;Haproxy
local0.* ~
This configuration will separate all HAProxy messages based on the $template to log files in /var/log. Now restart rsyslog to apply the changes.
# service rsyslog restart
Debian or Ubuntu:
To enable logging for HAProxy on Debian or Ubuntu, replace:
1
2
log /dev/log        local0
log /dev/log        local1 notice
with:
1
log         127.0.0.1 local0
Next, to configure separate log files for HAProxy, edit a file called haproxy.conf (or 49-haproxy.conf in Debian) in /etc/rsyslog.d/ with the following content.
1
2
3
4
5
6
$ModLoad imudp
$UDPServerRun 514 
$template Haproxy,"%msg%\n"
local0.=info ­/var/log/haproxy.log;Haproxy
local0.notice ­/var/log/haproxy­status.log;Haproxy
local0.* ~
This configuration will separate all HAProxy messages based on the $template to log files in /var/log. Now restart rsyslog to apply the changes.
# service rsyslog restart

2. Setting Defaults

The next step is to set default variables for HAProxy. Find the defaults section in /etc/haproxy/haproxy.cfg, and replace it with the following configuration.
1
2
3
4
5
6
7
8
9
10
11
defaults
log     global
mode    http
option  httplog
option  dontlognull
retries 3
option redispatch
maxconn 20000
contimeout      5000
clitimeout      50000
srvtimeout      50000
The configuration stated above is recommended for HTTP load balancer use, but it may not be the optimal solution for your environment. In that case, feel free to explore HAProxy man pages to tweak it.

3. Webfarm Configuration

Webfarm configuration defines the pool of available HTTP servers. Most of the settings for our load balancer will be placed here. Now we will create some basic configuration, where our nodes will be defined. Replace all of the configuration from frontend section until the end of file with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
listen webfarm *:80
       mode http
       stats enable
       stats uri /haproxy?stats
       stats realm Haproxy\ Statistics
       stats auth haproxy:stats
       balance roundrobin
       cookie LBN insert indirect nocache
       option httpclose
       option forwardfor
       server web01 192.168.100.2:80 cookie node1 check
       server web02 192.168.100.3:80 cookie node2 check
The line "listen webfarm *:80" defines on which interfaces our load balancer will listen. For the sake of the tutorial, I've set that to "*" which makes the load balancer listen on all our interfaces. In a real world scenario, this might be undesirable and should be replaced with an interface that is accessible from the internet.
1
2
3
4

stats enable
stats uri /haproxy?stats
stats realm Haproxy\ Statistics
stats auth haproxy:stats
The above settings declare that our load balancer statistics can be accessed on http://<load-balancer-IP>/haproxy?stats. The access is secured with a simple HTTP authentication with login name "haproxy" and password "stats". These settings should be replaced with your own credentials. If you don't need to have these statistics available, then completely disable them.
Here is an example of HAProxy statistics.

The line "balance roundrobin" defines the type of load balancing we will use. In this tutorial we will use simple round robin algorithm, which is fully sufficient for HTTP load balancing. HAProxy also offers other types of load balancing:
  • leastconn:­ gives connections to the server with the lowest number of connections.
  • source: hashes the source IP address, and divides it by the total weight of the running servers to decide which server will receive the request.
  • uri: the left part of the URI (before the question mark) is hashed and divided by the total weight of the running servers. The result determines which server will receive the request.
  • url_param: the URL parameter specified in the argument will be looked up in the query string of each HTTP GET request. You can basically lock the request using crafted URL to specific load balancer node.
  • hdr(name): the HTTP header <name> will be looked up in each HTTP request and directed to specific node.
The line "cookie LBN insert indirect nocache" makes our load balancer store persistent cookies, which allows us to pinpoint which node from the pool is used for a particular session. These node cookies will be stored with a defined name. In our case, I used "LBN", but you can specify any name you like. The node will store its string as a value for this cookie.
1
2
server web01 192.168.100.2:80 cookie node1 check
server web02 192.168.100.3:80 cookie node2 check
The above part is the definition of our pool of web server nodes. Each server is represented with its internal name (e.g., web01, web02). IP address, and unique cookie string. The cookie string can be defined as anything you want. I am using simple node1, node2 ... node(n).

Start HAProxy

When you are done with the configuration, it's time to start HAProxy and verify that everything is working as intended.

Start HAProxy on Centos/RHEL

Enable HAProxy to be started after boot and turn it on using:
# chkconfig haproxy on
# service haproxy start
And of course don't forget to enable port 80 in the firewall as follows.
Firewall on CentOS/RHEL 7:
# firewall­cmd ­­permanent ­­zone=public ­­add­port=80/tcp
# firewall­cmd ­­reload
Firewall on CentOS/RHEL 6:
Add following line into section ":OUTPUT ACCEPT" of /etc/sysconfig/iptables:
1
­A INPUT ­m state ­­state NEW ­m tcp ­p tcp ­­dport 80 ­j ACCEPT
and restart iptables:
# service iptables restart

Start HAProxy on Debian

Start HAProxy with:
# service haproxy start
Don't forget to enable port 80 in the firewall by adding the following line into /etc/iptables.up.rules:
1
­A INPUT ­p tcp ­­dport 80 ­j ACCEPT

Start HAProxy on Ubuntu

Enable HAProxy to be started after boot by setting "ENABLED" option to "1" in /etc/default/haproxy:
1
ENABLED=1
Start HAProxy:
# service haproxy start
and enable port 80 in the firewall:
# ufw allow 80

Test HAProxy

To check whether HAproxy is working properly, we can do the following.
First, prepare test.php file with the following content:
1
2
3
4
5
<?php
header('Content-Type: text/plain');
echo "Server IP: ".$_SERVER['SERVER_ADDR'];
echo "\nX-Forwarded-for: ".$_SERVER['HTTP_X_FORWARDED_FOR'];
?>
This PHP file will tell us which server (i.e., load balancer) forwarded the request, and what backend web server actually handled the request.
Place this PHP file in the root directory of both backend web servers. Now use curl command to fetch this PHP file from the load balancer (192.168.100.4).
$ curl http://192.168.100.4/test.php
When we run this command multiple times, we should see the following two outputs alternate (due to the round robin algorithm).
Server IP: 192.168.100.2
X-Forwarded-for: 192.168.100.4
Server IP: 192.168.100.3
X-Forwarded-for: 192.168.100.4
If we stop one of the two backend web servers, the curl command should still work, directing requests to the other available web server.

Summary

By now you should have a fully operational load balancer that supplies your web nodes with requests in round robin mode. As always, feel free to experiment with the configuration to make it more suitable for your infrastructure. I hope this tutorial helped you to make your web projects more resistant and available.
As most of you already noticed, this tutorial contains settings for only one load balancer. Which means that we have just replaced one single point of failure with another. In real life scenarios you should deploy at least two or three load balancers to cover for any failures that might happen, but that is out of the scope of this tutorial right now.