Cent OS 7 Connect to Wifi from CLI

10 October, 2017

So I am setting up my old Samsung laptop to be a VM host to have a bit of a home lab. Hopefully the laptop should be able to support 4 or 5 headless VM's. I have chosen to use Cent OS as the host, and will be using KVM for the virtualization. So far I have installed Cent OS on the laptop. Although being a VM host it really should be connected to the network via ethernet this is not practical for me so I will be connecting it over the WiFi and bridging this connection to the VM's.

Finding out how to connect a Linux box to a wifi network over CLI was the first thing I need to do after installing. There seems to be lots of different ides on how to do this, so I will go through the method that worked for me. First thing to do is to check if the OS has detected the WiFi on the device:



This should list the network adapters. Providing the WiFi adapter shows up we can then run:

nmcli dev wifi list


this will provide us with a list of the available WiFi networks. To connect to one we can simply run:

nmcli --ask dev wifi connect "Pretty fly for a Wi-Fi"

You will be asked for the password, then you will be given confirmation of the connection. You can always re-run 'sudo nmcli' to check the state of the connection. In case you want to remove the WiFi connection you cant run:

nmcli device disconnect iface wlp2s0

Where 'wlp2s0' in the interface you are disconnecting.

I found this to be a good reference for nmcli:


Use Nginx to redirect requests to servers based on domain

09 October, 2017

Nginx site configurations are stored under the folder: /etc/nginx/sites-available/ It is sufficient to just edit the default site as it is enabled by default. This however applies to any site configuration. A typical Nginx config file may look like:

server {
  listen 80;

  server_name hawkesbury.city;

  root /var/www/bookstack/public;
  index index.php index.html;

  location / {
    try_files $uri $uri/ /index.php?$query_string;

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;

This configuration tell Nginx to listen on port 80 for requests to the "hawkesbury.city" domain. All requests to this domain are served from the "root" directory. If the file requested ends in PHP we send it to Fastcgi for processing. This is great when you have a single domain and want to serve a website up for that domain. But we must go deeper!

Sending domain to Apache on the same server

Lets say we have a sub-domain like "mail.hawkesbury.city" which we are planning to use as a webmail interface. This being the case we are better off serving the page form Apache2 as it will be a very php heavy dynamic page. In the case that we only have the one server we can install Apache on the same server but set to use a different port. We can then tell Nginx to pass all requests to "mail.hawkesbury.city" to Apache over the Apache configured port. To do this we can create a new configuration file in Nginx or add a new server block to the existing configuration file containing the following:

server {
  listen 80;

  server_name mail.hawkesbury.city;

  location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

This tells Nginx to listen to requests for mail.hawkesbury.city on port 80 and pass those requests to port 8000 on localhost where Apache is listening. This is cool but I have different sub-domains on different servers and need to manage the load as such.

Sending domain to a separate server

As you probably guessed this is almost the same as the server block for sending a sub-domain to Apache on the same server. For the sub-domain "status.hawkesbury.city" the server block may look like:

server {
  listen 80;

  server_name status.hawkesbury.city;

  location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

This sends requests for "status.hawkesbury.city" to over port 80 which is a separate server running Apache set to listen on port 80.

SCCM PowerShell Task Sequence GUI – Image For Dayz

09 October, 2017

Playing around with our Task Sequences in SCCM I came to the realization that we have numerous post install tasks for optional software when an Imaged PC is for a particular department or entity. This is due to different departments having different software requirements. I started toying around with the idea of a software selection GUI as part of the Task Sequence that will allow customization of post install software in the aim to make all deployments ‘one touch’.

After some research and work I came up with a basic GUI with some customization:


However, this is still limited and does not cover all of our use cases, so we are still left with some post install tasks. After some more work I came up with the following ‘final’ GUI:


From this GUI we are able to choose all the combinations of required software for all departments and entities. No more post install tasks for any PC’s.

I had to do a few things to get this working as desired. But at the core of it, each of the tick boxes sets an MDT Task Sequence Variable. The variable weather ticked or not determines if the software installation step should proceed.


MDT Task Sequence Variables

The task sequence itself has some additional steps to create the variables. Each variable requires a separate step for the variable creation. I found the variables needed to be created in the task sequence before the PowerShell script that sets the variables values can be run.


All of the variable are set to False when created in the task sequence. The PowerShell script will later set the ones that are ticked to True.



Conditional Application Installations

We have the requirement that some applications are installed on every PC that we image, these applications are installed regardless of the options chosen. For the conditional application installations, I have created a separate step for each one.


Each of these steps simply installs a single application.


Under options I have set the condition that the Task Sequence Step only run if the corresponding variable is set to True.



The PowerShell Script

Creating GUI’s in PowerShell is tedious and annoying but there is plenty of information on how to do that so I will be glossing over the GUI creation part. The important part of the script is retrieving the value from the check box and injecting it into the Task Sequence variable. A minimal example how this works would be:

    #Get Variable from Form
    $installPDF = $pdfCheck.Checked #where $pdfCheck is the checkbox

    #set variable in the task sequence
    $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
    $TSEnv.Value("installPDFAnnotator") = "$($installPDF)"

The script needs to be made into an SCCM Package so that we can run the script from the Task Sequence. The source contents for the package should include the script to be run, ServiceUI.exe and any assets required by the script. In my case the company logo.


The package is nothing special, you only need to set the source files for it. And distribute it the required Distribution Points.


Incorporating the script into the task sequence requires adding a step ‘General > Run Command Line’. The command line step should look similar to the following:


The two main things to note here is the command line is pointing to the PowerShell script that is part of the package and that the package is selected. You just need to make sure that you declare all the MDT Task Sequence variables before running the PowerShell step “Prompt For Image Configuration”


And that should be it!

Maze generation using Prims Algorithm

09 October, 2017

I have recently been playing around with the idea of generating mazes. This is mostly due to the tower defense game I am working on and generating mazes is a very interesting topic. Below is an implementation of the Prims algorithm to generate a maze. The code for this is on my git: https://github.com/NathanKewley/Mazes

Maze Seed:


Find best solution to any maze

09 October, 2017

I am currently working on a Tower Defence game with Caleb and Natasha Op’tLand. As part of this game the player will be able to place down towers in such a way that it creates a maze for the enemies to traverse. The enemies should be able to find the best possible solution to any maze that the player creates. The game is based on a 2d grid, A map may look like the following with some towers and rocks blocking the enemy path.


The numbers represent the path the enemies will use to traverse this maze.

The method I have used to solve the maze is by no means the most efficient but it will however always find the best possible solution. This is done by assigning values to tiles beginning with the starting tile and finishing when the end tile has a value assigned. Firstly the starting tile is assigned the value of 1. Tiles adjacent to this are given a value of 2 and then 3 and so on until the end tile is assigned a value.


Values being assigned to tiles until the end tile is reached.

Once the end tile has a value assigned (In this case 12) we can use this information to backtrack from the end tile to find the solution. This is done by looking for tiles adjacent to the end tile with a value of one less (11). This is repeated until the starting tile is found.


This is the method I am currently using to find the best possible solution for any maze.

Procedurally generating a solar system

09 October, 2017

Procedural generation has always been of interest to me, and solar systems are cool. So I decided to explore procedural generation by procedurally generating a solar system! Below is a solar system that has been generated based on a pseudo random seed.


To procedurally generate anything we first need a seed to base our generation on. We will seed the random number generation to our seed. Once we have a seed the rest of the generation will be based on this. This means that the same seed will always generate the exact same solar system, so if there is a solar system that is particularly nice the seed can be saved to re-generate the exact same solar system at any given time. I have written this in HTML5 using the phaser framework and thus my code to generate and set a random seed is as follows:

Math.seed = Math.floor(Math.random() * (89898989898989898989 - 29898989898989898989 + 1)) + 29898989898989898989;

Where Math.seed is the value that we will use as our unique seed.

Math.randomSeed = function(max, min) {
    max = max || 1;
    min = min || 0;
    Math.seed = (Math.seed * 9301 + 49297) % 233280;
    var rnd = Math.seed / 233280;
    return min + rnd * (max - min);

Math.randomSeed is the random function that we will use to generate pseudo random numbers based on the seed generated and stored in Math.seed. Of particular note is:

Math.seed = (Math.seed * 9301 + 49297) % 233280;

This is a general math equation to generate pseudo random numbers, more info on this is available at: http://stackoverflow.com/questions/33716998/why-does-seed-9301-49297-233280-233280-0-generate-a-random-number

Once the seed is generate we can begin to generate our solar system. Our solar system consists of 4 elements: The sun, Planets, Moons and Stars. Each of these are generated based on the seed. Some things like the planets are affected by the sun that is generated. Due to this we must generate the solar system in the following order:
⦁ Stars – only affected by the seed
⦁ Sun – only affected by the seed
⦁ Planets – Affected by the heat of the sun and distance from sun. More on this later
⦁ Moons – Affected by the distance from the planet and the climate of the planet
We will look at how each element is generated.

Generating the stars

The stars are probably the easiest to generate as the stars are based on pre-defined images. There are 9 possible images that will be used for the stars, they are all similar with a slyghtly different layout:


The image to be used is chosen at random based on the seed:

  this.type = parseInt(Math.randomSeed(1,10));
  this.sprite = game.add.sprite(960, 960, 'stars'+ this.type);

Once one of the 9 images is chosen we set a random rotation for it in 90 degree intervals.

  var rotation = parseInt(Math.randomSeed(1,5));
  this.sprite.angle = rotation*90;

In doing this we get a total of 36 different possibilities for the layout of the stars as each of the 9 images have 4 different possible rotations.

Generating the Sun

The Sun is the core of the solar system and will have an effect of every planet and moon that is generated. To generate the Sun the first thing we need to do is define all the properties that a sun has. For this project I decided a sun was determined by its Type, Size, Heat and Sprite. In this case Type is an arbitrary value to add some extra variation. Size is pseudo random and Heat is based on both the Type and Size of the sun. These 3 values are set using our random function defined earlier.

	this.type = parseInt(Math.randomSeed(1,6));
	this.size = parseInt(Math.randomSeed(1,9));
	this.heat = (this.size*this.type*5)+30;

The last thing we need to do is set a sprite for the sun. The sun much like the stars have a pre-defined set of images that can be used:


However the sprite that is chosen to represent the sun is based on the sun type and as a result also corresponds to the heat of the sun:

this.sprite = game.add.sprite(10, 10, 'sun'+this.type);

Generating the planets

Much like the sun to generate planets we must first determine all the properties that make up a planet. I decided each planet should have a Size, Distance, Water Level, Speed, Temperature, Climate, Name and Sprite. Each value is determined based on the Seed and also often the Sun that has been generated.

The Size, Distance and Water Level are based only on the Seed:

	this.size = parseInt(Math.randomSeed(1,4));
	this.distance = parseInt(Math.randomSeed(3,9));
	this.waterLevel = parseInt(Math.randomSeed(1,10));

The Speed is based on both the Seed and Distance:

	this.speed = (parseInt(Math.randomSeed(10,25)))-this.distance;

The planets temperature is base on the Seed, Heat of the Sun and Distance from the sun:

	this.temp = this.sunHeat – (this.distance*8);

The climate is based on both the Water Level and Temperature of the planet. I wrote a separate function to work out the climate of the planets based on these values.

planetClass.prototype.getClimate = function(water, temp){
	var climate;
	if(water < 4){
		if(temp < 1){climate = "plains"}
		else if(temp < 41){climate = "desert"}
		else{climate = "fire"}
	}else if(water < 7){
		if(temp < 1){climate = "earth"}
		else if(temp < 41){climate = "earth"}
		else{climate = "tropic"}
		if(temp < 1){climate = "ice"}
		else if(temp < 41){climate = "tropic"}
		else{climate = "gas"}
	return climate;

Each planet should have a name to make is more unique. Each name is based on the Seed and a pre-defined list of names that are available. Depending on the Seed a random name is chosen and a random number added to the name.

planetNames = ["Zeus","Hera","Poseidon","Demeter","Ares","Athena","Apollo","Artemis","Hephaestus","Aphrodite","Hermes","Dionysus","Hades","Hypnos","Nike","Janus","Nemesis","Iris","Hecate","Tyche"]

this.name = parseInt(Math.randomSeed(0,20));
this.name = planetNames[this.name] + parseInt(Math.randomSeed(0,200));


The last thing to do is set an appropriate sprite for the planets. Again this is chosen from a pre-defined selection of planet sprites.

The sprite that is chosen for the planet is a direct result of the climate of the planet:

this.sprite = game.add.sprite(10, 10, this.climate);

Generating the Moons

Again the generate the moons we need to define the properties that a moon has. I decided each moon needed a Size, Speed of Orbit, Type, Distance and Water Level.

The Size, Speed of Orbit and Type are directly related to the Seed:

	this.moonSize = parseInt(Math.randomSeed(1, 4));
	this.speed = parseInt(Math.randomSeed(5, 10));
	this.type = parseInt(Math.randomSeed(1,4));

The distance is based on the distance of the moon from the sun.
The Water Level is based on the Water Level of the planet

The Sprite for the moons is chosen from the same selection of sprites as the planets they are however scaled down. The sprite chosen for the moons directly corresponds to the Type of moon:

		case 1: this.sprite = game.add.sprite(10, 10, 'desert'); break;
		case 2: this.sprite = game.add.sprite(10, 10, 'fire'); break;
		case 3: this.sprite = game.add.sprite(10, 10, 'plains'); break;

Adding an AI to battle for control of the Solar System Generated

For a little bit of extra fun I created a basic AI that will battle for control over the generated Solar system. This becomes Red vs Green where each AI is assigned a starting planet. Each planet is given a “ship production rate” to generate ships. The AI will send ships from planets it owns to try capture planets it does not own in an attempt to control the entire Solar System.


The full source for this project is available on my Github at: https://github.com/NathanKewley/Procedural_Space

Home ← Older posts