FizzBuzz in C++

Something really out of my comfort zone here. I completed the C++ course on codecademy and did what I normally do when I’m learning a new programming language. I try to make the FizzBuzz game in as many different ways as I can to get used to the syntax and different operations.

This is was the first attempt at it and I think the easiest to do. It’s not clean but I like learning and you need to make mistakes in order to really learn something:

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 101; i++){
        if (i % 5 == 0 & i % 3 == 0){
            std::cout << "FizzBuzz\n";
        }else if (i % 5 == 0){
            std::cout << "Fizz\n";
        }else if (i % 3 == 0){
            std::cout << "Buzz\n";
        }else{
            std::cout << i << "\n";
        }
    }
}

My second iteration was a little more sophisticated and would make any alterations to the rules of FizzBuzz easier to account for:

#include <iostream>
#include <string>
using namespace std;

int main() {
    for (int i = 0; i < 101; i++){
        string output;
        output = "";
        
        if ( i % 5 == 0 ){ output += "Fizz"; }
        if ( i % 3 == 0 ){ output += "Buzz"; }
        if ( output == "" ){ output = to_string(i); }
        cout << output << "\n";
    }
}

No doubt that I will be jumping back to create new ways when I have the time. Let me know what you come up with.

Enjoy!

Submitting a CSR for Signing by a CA

Bit of a weird post today, I had a need to update the SSL certificate for an old DRAC (Dell Remote Access Controller) 5 module. This is just an overview of what I did.

First I logged into the DRAC and went to System > Remote Access > Configuration > SSL > Generate a new certificate signing request (CSR). In here I entered my details and put the address I would use for connecting to the server as the common name.

After I clicked “Submit”, I got a small txt file called “csr.txt”. I then needed to get this signed by a Certificate Authority (CA) so that I could get an actual certificate file. Note that the format you want for DRAC 5 is .cer

The CA I used for this was “getacert“, they’re a bit “ghetto” with their site not even using the technology that they provide… kind of ironic but I was desperate.

On this site I entered the contents of my csr.txt file and clicked “Submit CSR“. This them gave me the certificate file, signed by getacert.

Finally, I went back into the DRAC management webpage (in the same location as pointed out earlier) and selected the option to “Upload Server Certificate” Now just select the .cer file you got from the CA and click submit. This will cause the management site to go down for a couple of minutes whilst it configures the new certificate.

After this you should be all set. Enjoy!

Desktop Icons Disappeared

This is a weird issue I encountered where even though I had tried restarting explore.exe and disabled and enabled desktop icons, they still weren’t showing. Even weirder was the fact that only some of them were showing whilst others weren’t. Weird indeed.

Here you can see that the text file “Test” isn’t showing on my desktop even though it is in the desktop folder:

Desktop Icons

 

 

 

Desktop Folder #1

 

 

 

 

 

To remedy this issue, what I had to do was right click on the desktop, go into “Sort By” and select “Name“. This jiggled the desktop and made the text file appear. Not sure what causes this at all.

Hopefully someone finds this useful, I know I could have done with this information at the time. Enjoy!

Linux Service Restart Script

I required a way to easily restart some common services on my Raspberry Pi running Plex since I kept forgetting whether I needed to use the:

service NAME restart

syntax, or the:

systemctl restart NAME

syntax for the services.

So I created a bash (.SH) file that would allow me to chose which service to restart without having to remember the different commands. Below are my requirements:

  • Give a list of services
  • read which service to restart
  • Restart service
  • restart script

You can see my script below:

#!/bin/bash

restart_script () {
 bash /home/pi/restartservices
}


NUMBER=1
echo "$(tput setaf 3)Following services available:"
echo ""

#LIST SERVICES

for SERVICE in "VSFTPD" "Plex" "VNC Server" "LightDM"
 do
 echo "$(tput setaf 3)($NUMBER)$(tput setaf 7)$SERVICE"
 NUMBER=$[$NUMBER +1]
done

echo ""
echo "$(tput setaf 3)(E)xit:"

read -p "$(tput setaf 3)Chose a service to restart:" READINPUT

if [ $READINPUT = "1" ]
 then
 echo "$(tput setaf 3)Restarting VSFTPD Service...."
 sudo service vsftpd restart
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "2" ]
 then
 echo "$(tput setaf 3)Restarting Plex Service...."
 sudo service plexmediaserver restart
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "3" ]
 then
 echo "$(tput setaf 3)Restarting VNC Server...."
 sudo service vncserver restart
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "4" ]
 then
 echo "$(tput setaf 3)Restarting LightDM Service...."
 sudo systemctl restart lightdm
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "E" ] || [ $READINPUT = "e" ]
 then
 echo "$(tput setaf 3)Exiting Script"
 exit 1
else
 echo "$(tput setaf 1)INVALID RESPONSE - WILL LOOP UNTIL RESPONSE IS VALID!"
 restart_script
fi

Rather than having to type in the name of the service I wanted to restart, I used a for loop to add a number to each entry in the services list. This can be found from line 14 to line 19.

Then to run my script from a terminal session, I simply type “./restartservices”

Enjoy!

Create, Store & Use Encrypted Passwords With PowerShell

This is just a neat little “tactic” I use when I need to connect to the same machine over and over again but don’t want to drive myself insane with having to constantly enter the same username and password. For example, when testing a script.

First you need to enter your password, in plain text, into this script so that it can get the password. This a perfectly safe as it will only be at this point where the password is in plain text.

$password = "PUT PASSWORD HERE" | ConvertTo-SecureString -AsPlainText -Force

This gets the password that you just entered and encrypts it and also puts it into the variable “password”

Now you need to convert the password to an encrypted string of characters using the below command:

$Password2 = $password | ConvertFrom-SecureString | Out-File "PATH TO TEXT FILE TO STORE PASSWORD"

This puts the encrypted password into the text file for later use.

Now, whenever you need to connect to a machine, you can put this into a variable along with the username. Then put them together into a credential and away you go:

$Username = "DOMAIN\username"

$EcryptedPassword = Get-Content "LOCATION TO TEXT FILE" | ConvertTo-SecureString

$Credential = New-Object System.Management.Automation.PSCredential($Username, $EncryptedPassword)

This builds the credential which you can now use with something similar to below:

Invoke-Command -Credential $Credential -ScriptBlock {echo "test"} -ComputerName "COMPNAME" -Authentication CredCSSP

Enjoy!

Windows Favourites Folder Spelt “Favorites”

Something a little off topic today. Windows naming is weird… that’s it. I found this when trying to run a RoboCopy script to back-up my favourites “Internet Explorer” (Award winning, grama nominated, amazing browser).

This is what my script for RoboCopy used to look like:

robocopy C:\Users\harwoodm\Favourites d:\robocopy\Favourites /MIR /R:1 /W:5 /FFT /LOG:"C:\users\harwoodm\robocopy logs\favourites.txt" /TEE /NP /NFL /NDL

I was getting Error 2, path not found.

Little did I know that, even though in the Explorer it’s spelt “Favourites” the path has it as “Favorites”

EVIDENCE!!!

Favorites

Does this make any sense to you, doesn’t to me…

Anway, hope this helped. Enjoy!