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.

What Should You Do when the Apache Mesos Web UI Keeps Refreshing and Sending a Pop-up “Failed to connect to …:5050”?

Problem scenario
You deployed Apache Mesos. The web UI is having problems. You see the error "Failed to connect to x.x.x.x:5050." What should you do?

Solution

  1. Go to the back-end of the Apache Mesos server. Run this command: sudo systemctl stop mesos-master
  2. sudo find / -name mesos-master.sh
  3. Change directory into the parent of the "bin" directory that houses the mesos-master.sh as found above.
  4. Find the internal IP adress (e.g., with ip addr show | grep eth0 | grep inet).
  5. Run this command but substitute x.x.x.x with the internal IP address:
sudo ./bin/mesos-master.sh --ip=x.x.x.x --work_dir=/var/lib/mesos --hostname=x.x.x.x

6. Go back to your web browser. Refresh your web browser.

How Do You Create a REST API Endpoint?

Problem scenario
You want a REST API endpoint. How do you create a URL that is a proof-of-concept to trigger a Python program?

Solution
Prerequisites
i. You have installed Python 3. If you are using CentOS/RHEL/Fedora, run this: sudo yum -y install python3
ii. You need a web server installed. If you do not have one, install Apache web server.
iii. You must have PHP installed. To install it, run this command: sudo yum -y install php

Procedures
1. Create a file in /home/ec2-user/ called good.py with the following content:

# This was adapted from https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file handler
handler = logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)

# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the file handler to the logger
logger.addHandler(handler)

logger.info('Hello from contint')

2. Create a file in /var/www/html/ called good.php with the following content:

<html>
<body>

HELLO!

</body>
</html>

<?php

   $command = escapeshellcmd('/usr/bin/python3 /home/ec2-user/good.py');
   $output = shell_exec($command);
   echo $output;

?>

How Do You Write a Java Program to Read in Letters and Numbers?

Problem scenario
You want to create a basic Java program that can read alphanumeric input interactively. How do you write such a program?

Solution
1. Verify the "javac" command works. Run "man javac" to see the man page. If you do not get a man page, install the Java development tools. On a RedHat derivative, run this: sudo yum -y install java-devel

2. Create a contint.java file with the content below. Use the usage instructions in the header comments.

/* This program was written by continualintegraiton.com
You should call it contint.java.  compile it with "javac contint.java" and run the byte code with "java contint"
It shows how to read in letters and numbers into a Java program */

import java.util.Scanner;

public class contint{
    public static void main(String args[]){
    System.out.println("Enter some characters and press enter:");

    Scanner ascanitem= new Scanner(System.in);
    String secondpart= ascanitem.nextLine();
    System.out.println(secondpart);
    System.out.println(" ");
    System.out.println("Enter some numbers and press enter");
    int number= ascanitem.nextInt();
    System.out.println(number);
    }
}

3. Run these commands:
javac contint.java
java contint

In Python, What Are Some Disadvantages to Using os.execlp to Fork a Process?

Question
In Python you are familiar with importing the os module and using different exec variations. What are some reasons that you would not use os.execlp?

Answer
1. If you use os.execlp to call another program, that program is more likely to return "Killed". The resources of the child process are, by default, more limited in part because the fork operation is expensive from a system's resources perspective. Here is an example of two programs (where bar.py calls foo.py). If you run the top program by itself, it will work on a low-powered system:

# Program #1 (foo.py)
import os, datetime, time
t1 = datetime.datetime.now()

def wastetime(n):
  x = 0
  for i in range(n):
    x = x + i

wastetime(30000000)

t2 = datetime.datetime.now()
t3 = t2 - t1
print("Time format is in hours:minutes:seconds:seconds_decimals")
print(t3)
# Program #2 (bar.py)
import os, cProfile

def callprog():
  os.execlp('python', 'python', 'foo.py')

cProfile.run('callprog()')

But if you run the bottom program, when it is in the same directory as the top program, it will not work. The bottom program will returned "Killed" -- even though all it is doing is running the top program. (You may want to read How Do You Fix a Python Program that Returns “Killed”?)

2. You cannot see the output of cProfile with the called program. cProfile will give you statistics on function calls. If you could run the called program directly, you can benefit from seeing the cProfile output. But if the program is invoked from an os.execlp() call, the called program's cProfile output will not display.

Here is a program that will display cProfile output to help illustrate why you would not use os.execlp():

# You can name it test2.py and run it with "python test2.py"
import os, datetime, time, cProfile
t1 = datetime.datetime.now()

def wastetime(n):
  x = 0
  for i in range(n):
    x = x + i

cProfile.run('wastetime(20000000)')

t2 = datetime.datetime.now()
t3 = t2 - t1
print("Time format is in hours:minutes:seconds:seconds_decimals")
print(t3)

# If you have a second program to call that program above, no cProfile output will be visible.

How Do You Install Java so You Can Compile Programs on a RHEL Server?

Problem scenario
You have installed Java. But you cannot use the javac command. You get errors like this: "-bash: javac: command not found"

How do you install Java so you can compile programs on a RedHat Linux server?

Solution
Run this command: sudo yum -y install java-devel

If you want to do this on a Debian or Ubuntu Linux server, see this posting.

How Do You Reclaim Swap Space on a Linux Server?

Problem scenario
You mounted 2 GB of swap space (virtual memory) called 2GB.swap. (You followed these directions. Later you added RAM to the server.) This 2 GB swap space is still mounted. How do you clear it and repurpose it on your hard drive?

Solution
1. Run this command: sudo swapoff -a
2. Modify the /etc/sysctl.conf file. (You may want to back it up before you make a change.) Here is what you need to do to change it:
Run this command: sudo vi /etc/sysctl.conf
# eliminate the "vm.swappiness=10" stanza

3. Modify the /etc/fstab file. (You may want to back it up before you make a change.) Here is what you need to do to change it:
Run this command: sudo vi /etc/fstab
#eliminate the "/mnt/2GB.swap none swap sw 0 0"

4. Run these commands:
cd /mnt
sudo rm -rf 2GB.swap

How Do You Get a Raspberry Pi to Work Again after Two GPIO Pins Were Connected When They Should Not Have Been?

Problem scenario
You recently connected a jumper connector to two GPIO pins accidentally on your Raspberry Pi. Now your Raspberry Pi will not start, and it seems fried. There is a red light that is on. There is no flashing light and no green light. There is no output to the monitor. You are not sure if the Raspberry Pi is wasted.

Solution
1. Remove the jumper connector from the GPIO pins. Either get a MicroSD card from a working Raspberry Pi and skip the rest of these steps (to test the Raspberry Pi), or get a new blank MicroSD card. It may be that the original MicroSD card is not usuable.

2. Open a web browser and go to http://downloads.raspberrypi.org/NOOBS_latest. Download the .zip file. (To download the large zip file to a specific location, see this posting if you have a Windows desktop.)

3. Place the MicroSD card into a card reader so you can access it with the Windows desktop.

4. Put the contents of the zip file (step #2) on to the MicroSD card (step #3).

5. Now the MicroSD card should allow the Raspberry Pi to work.