How Do You Install Grails on Any Type of Linux server?

Problem scenario
You want to install Grails on a Linux server. What do you do?

Solution
Prerequisite
Your server has access to the internet.

Procedures
Run these commands:

sudo curl -s https://get.sdkman.io | sudo bash
sudo su -  
sdk install grails

# We know this isn't a best practice to install things as root, but prevalent directions with the "source" command and an init.sh file don't always work with sudo.

How Do You Configure Linux CentOS 6.x to Accept SSH Connections?

Problem scenario
You try to use PuTTY to connect to a Linux CentOS 6.x server. You get the message "Network error: Connection refused." What should you do?

Solution
Prerequisite
This assumes that there is no software or hardware firewall blocking port 22. You may want to use nmap or a Windows PowerShell port tester to verify port 22 is open. If iptables is blocking port 22, this will not work until you reconfigure iptables.

Procedures

  1. Go to the Linux CentOS server.
  2. Modify the /etc/ssh/sshd_config file. Make sure that the
    "PasswordAuthentication" and "ChallengeResponseAuthentication" stanzas are set to yes. Make sure that they are not commented out (with a leading "#"). Make sure that the corresponding stanzas for "no" are commented out (with a leading "#").
  3. Run this command: sudo service sshd restart
  4. If you want the ssh daemon to be listening upon every reboot, create a crontab entry with the root user like this: @reboot /sbin/service sshd restart

How Do You Send an Email from a Postfix Email Server with A “Reply-to” Email Address of Your Choosing?

Problem scenario
You are using Linux. You want to send emails from your server directly (e.g., from the command prompt using telnet and helo commands) with a customized "Reply-To" email address. How do you do this so the recipient clicks "reply" the "To:" field will be pre-populated.

Prerequisite
Install Postfix. See one this posting if you are using Linux SUSE; see this posting if you have a CentOS/RedHat/Fedora distribution. If you have an Ubuntu or Debian distribution, we recommend trying to use the CentOS/RedHat/Fedora posting, but be prepared to deviate from the directions.

Procedures
1. Edit the /etc/postfix/main.cf file in three ways.
1.a. Find the "mydomain" stanza. Uncomment it out (remove the leading "#"). Set it to the domain you want like this (but replace continualintegration.com with the domain name that you want the "Reply-To" to have):
mydomain = continualintegration.com

1.b. Find the "inet_interfaces = all" stanza and uncomment it out (remove the leading "#").

1.c. Append this stanza to the very bottom of the file:
smtp_generic_maps = hash:/etc/postfix/generic

2. Edit (or create) this file /etc/postfix/generic so it has these stanzas (but replace continualintegration.com with the domain you want the "reply-to" to have, and "no-reply" with the destination user that you want the email address to have):

root@continualintegration.com   no-reply@continualintegration.com
@continualintegration.com       no-reply@continualintegration.com

3. Run this command to have the changes take effect: sudo postmap /etc/postfix/generic

An Alternative Way To Send Email
Another way to do this is with Python; to learn how to use Python and not install anything else, see this external posting.

How Do You Troubleshoot “550 5.1.1 johndoe@example.com: Recipient address rejected: User unknown in local recipient table”?

Problem scenario
You are using Postfix. You are trying to send emails directly from the Postfix server. After you use telnet and helo commands, you enter a recipient like this:

rcpt to: johndoe@example.com

But you get this error:

"550 5.1.1 johndoe@example.com: Recipient address rejected: User unknown in local recipient table"

What should you do?

Root cause
If you change the /etc/postfix/main.cf file to have emails use a "reply-to" of example.com, the Postfix server will think that it is the destination locally. So you will not be able to send emails to example.com provided that the "reply-to" domain uses "example.com." It is the only domain you cannot send to.

Solution
Configure the "reply-to" to be a different domain or use a different Postfix server for sending emails.

To configure a new Postfix server, see this posting if you are using Linux SUSE. See this posting if you are using a RedHat (e.g., CentOS or Fedora) distribution. For Debian/Ubuntu, we recommend trying to use the RedHat posting.

In Python How Do You Print Three Integers Separated by a Space on the Same Line?

Problem scenario
You want to print three integers. You do not want to convert them to be part of a string. You want a space to separate each integer. How do you do this in Python?

Solution
Assuming your variables are x, y, and z, and they are integers, use a line like this:

print(x, y, z)

Here is a complete program that proves it works:

x = 5
y = 3
z = 100
print(x, y, z)
print(type(x))
print(type(y))
print(type(z))

How Do You Write a Python Program to Download the Images of a Website?

Problem scenario
You want to download pictures (e.g., .png, .jpeg etc.) from websites. How do you use Python to download such files?

Solution
Use this program with a subdirectory in the directory that this program resides to receive the picture files.

"""
dwldimages.py
    Downloads all the images on the supplied URL, and saves them to the
    specified output file ("/test/" by default)

Usage:
    python dwldimages.py http://example.com/ /tmp/test/  # where /tmp/test/ is the directory you want to save the image files to.

# Mostly taken from https://stackoverflow.com/questions/257409/download-image-file-from-the-html-page-source-using-python
"""

from bs4 import BeautifulSoup as bs
import urlparse
from urllib2 import urlopen
from urllib import urlretrieve
import os
import sys

def main(url, out_folder="/test/"):
    """Downloads all the images at 'url' to /test/"""
    soup = bs(urlopen(url))
    parsed = list(urlparse.urlparse(url))

    for image in soup.findAll("img"):
        print "Image: %(src)s" % image
        filename = image["src"].split("/")[-1]
        parsed[2] = image["src"]
        outpath = os.path.join(out_folder, filename)
        if image["src"].lower().startswith("http"):
            urlretrieve(image["src"], outpath)
        else:
            urlretrieve(urlparse.urlunparse(parsed), outpath)

def _usage():
    print "usage: python dwldimages.py http://example.com [outpath]"

if __name__ == "__main__":
    url = sys.argv[-1]
    out_folder = "/test/"
    if not url.lower().startswith("http"):
        out_folder = sys.argv[-1]
        url = sys.argv[-2]
        if not url.lower().startswith("http"):
            _usage()
            sys.exit(-1)
    main(url, out_folder)

How Do You Assign a Variable in Groovy to Be an Integer Value?

Problem scenario
A Groovy program is returning an incorrect value for an integer variable. What is wrong?

Possible solution #1
Check the logic of your program. Use the println command to see the earliest point when this variable is assigned an incorrect value. This helps you pinpoint the problem.

Possible solution #2
Is the variable being assigned a number in quotes like this?

int i = "0"

You should remove the quotes from the numeral zero.

Why Cannot You Ping an IP Address of an AWS Server when the Security Group Should Allow for Pinging?

Problem scenario
In AWS you manually added a Security Group rule for the source of a given IP address. This IP address is either the internal or external IP address. You can ping one of them (either the internal or external IP address). Why cannot you ping each IP address?

Solution
Check to see if a firewall is running on the Linux server. Run these commands:

sudo ps -ef | grep firewalld
sudo systemctl status firewalld
sudo ps -ef | grep ufw
sudo ufw status
sudo iptables -L
nmap -P0 127.0.0.1

Secondly the root cause could be human error. Use the web browser to search for the rule in the AWS Console's section for Security Groups. Rather than make a human, manual check with your eyes, ensure that the web browser can find the IP address as it appears on the server. In other words highlight the IP address with your mouse and copy it into a search field in the web browser. Make the web browser find this IP address. This prevents manual error.

Thirdly, ensure that the Security Group's rule is for "Inbound" connections from the source IP address that you are trying to ping from. Outbound exceptions will not help you. The root cause of this is human error.

Fourthly, is the server in a VPC? If so you may need to adjust the NACL (Network Access Control List) to allow connectivity from your workstation or server to access the VPC itself.

How Do You Run an Ansible Playbook?

Problem scenario
You want to run an Ansible playbook to push configuration changes down (e.g., transfer files to managed nodes). How do you do this?

Solution
Prerequisites
This assumes that you have set up Ansible. If you need directions for deploying an Ansible control server, see this posting. If you need directions for deploying a managed node after the central Ansible server has been set up, see this posting.

Procedures
1. Log into the Ansible control server.
2. Create a file called contint.yaml, and place this text inside it:

- hosts: all
  tasks:
  - copy:
      src: /tmp/good.txt
      dest: /tmp/foo.conf
      owner: ec2-user
      group: ec2-user
      mode: 0644

3. Run this command: ansible-playbook contint.yaml

How Do You Solve the Linux Error “hostname: Name or service not known”?

Problem scenario
You run hostname -f and you get this error: "hostname: Name or service not known"

How do you solve this problem?

Solution
Do these things in this order:

  1. Verify you have the hostname in the /etc/hostname file.
  2. Verify the name above matches what is in /etc/hosts for the local IP address.
  3. Try rebooting the server.

Optionally, to read more about how the results are determined, see click here.