Friday, December 27, 2019

Discover External IP from Command Line

Getting your external IP address from the command line in both windows and linux is very easy with the OpenDNS resolver and a couple of easy commands found here.

In short, use dig on any unix variant (Linux, BSD, MacOS) as it is usually installed by default

dig +short myip.opendns.com @resolver1.opendns.com

and on Windows, if you don't have dig installed you can use nslookup

nslookup myip.opendns.com. resolver1.opendns.com

Saturday, November 30, 2019

Raspberry Pi Headless WiFi Setup

After flashing Raspbian to an SD card for the Raspberry Pi, there are a few things to do in order to make it a headless install. The first is to enable SSH. For this place an empty file named ssh with no extension to the root of the boot disk. In the terminal window, run this command:

touch /Volumes/boot/ssh

The next step is to add your wifi settings to a file on the boot disk called wpa_supplicant.conf. In the terminal window, run this command:

touch /Volumes/boot/wpa_supplicant.conf

then paste the following into it (adjusting for your ISO 3166 alpha-2 country code, network name and network password):

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="SSID"
    psk="PASSWORD"
}

Edit the file on the boot drive that was created and paste the above into it (adjusting for the name of your country code, network name and network password). The Raspberry Pi is now ready to be plugged in and will connect to your network. From here, you can log into your router and find the IP address or use a network scanner application and connect with the default pi username and password to complete the setup.
 
UPDATE: I created a small bash script to create the files and update the wireless ssid and password to speed up the process after flashing the SD card:
 
#!/bin/bash

SSID=${1}
PSK=${2}
VOLUME=${3:-/Volumes/boot}

FILES=('ssh' 'wpa_supplicant.conf')

for F in ${FILES[@]}
do
  printf "checking for ${F}\n"
  if [ ! -f $/{VOLUME}/${F} ]; then
    touch ${VOLUME}/${F}
  fi
done


cat > ${VOLUME}/wpa_supplicant.conf <<EOF
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="${SSID}"
    psk="${PSK}"
}
EOF

Saturday, April 6, 2019

Shred USB Drive

To reset a complete drive...

Code:

sudo shred -fvzn0 /dev/sdX

where /dev/sdX is the drive to be cleared out.

Note that this will wipe all of the drive including the boot sector, and the system partition as well. If you accidentally use it on /dev/sda or your boot drive this will require you to re-install the boot sector and the main partition of the drive.

As for the switches in use above ...

f = force
v = verbose
z = zero  
n0 = 0 iterations   


*z performs a single pass of zeros written to the drive sometimes referred to as a "factory reset".
*n0 means do not do any passes using random data. This is very low data security but speeds up the resetting process drastically. Increase this value for higher data security protection if needed. If this value is NOT set then shred by default will do 3 passes of random data (very slow for the command to complete its run).

Simple VBox Creation Script

There has been something on my list for sometime and that is to preseed an installation of Ubuntu and VirtualBox. I've been wanting to automate the installation of a virtual machine using VirtualBox and configure everything that I need from CPU to Memory to Forwarded ports. Mostly I deal with Ubuntu Linux machines and end up installing a VM over and over ... from messed up configurations to maybe something different that I want to try. The problem with this is that I spend too much time configuring the settings, and installing the OS. So I finally decided to create something that would do most of the work for me. Now I know you're saying why not just learn to use Packer and do it that way where you create an image that you use multiple times and can port elsewhere. Well, mostly this is for testing and getting things to work. It has it's benefits and it has it's draw backs, but for the problem that I am trying to solve at the moment it works. Here is the script to create the VM, and if you have an automated iso for the install it will start that and then shutdown when it is complete.
 
    #!/bin/bash

    #TODO - ADD Input for VMName
    NAME="ubuntu"
    VMDIR="/home/$USER/Documents/vm/$NAME/"
    DVD="/home/$USER/Documents/vm/iso/ubuntu-18.04-mini-amd64.iso"

    echo -e "Creating VirtualBox VM"
    VBoxManage createvm --name $NAME --ostype Ubuntu_64 --register

    if [ ! -d "$VMDIR" ]; then
      mkdir /home/$USER/Documents/vm/$NAME/
    fi

    echo -e -e "\nCreating VM Disk VDI file"
    VBoxManage createmedium --filename $VMDIR/$NAME.vdi --size 40960

    echo -e "\nAttaching storage"
    VBoxManage storagectl $NAME --name SATA --add SATA --controller IntelAhci
    VBoxManage storageattach $NAME --storagectl SATA --port 0 --device 0 --type hdd --medium $VMDIR/$NAME.vdi
    VBoxManage storagectl $NAME --name IDE --add ide
    VBoxManage storageattach $NAME --storagectl IDE --port 0 --device 0 --type dvddrive --medium $DVD

    echo -e "\nModify VM Settings"
    VBoxManage modifyvm $NAME --memory 2048
    VBoxManage modifyvm $NAME --ioapic on
    VBoxManage modifyvm $NAME --boot1 dvd --boot2 disk --boot3 none --boot4 none
    VBoxManage modifyvm $NAME --cpus 1
    VBoxManage modifyvm $NAME --audio none
    VBoxManage modifyvm $NAME --usb off
    VBoxManage modifyvm $NAME --usbehci off
    VBoxManage modifyvm $NAME --usbxhci off
    #VBoxManage modifyvm $NAME --nic1 bridged --bridgeadapter1 wlan0 --nic2 nat
    VBoxManage modifyvm $NAME --nic1 nat

    echo -e "\nAdd SSH port forwarding to 2222"
    VBoxManage modifyvm $NAME --natpf1 "guestssh,tcp,,2222,,22"

    #Start VM
    VBoxManage startvm $NAME --type gui


This will launch the VM and mount the iso that is used for an unattended install with SSH keys and forwards the SSH port to the localhost on 2222 where you can connect using a private key or password similar to this: ssh -i ../.ssh/id_rsa root@127.0.0.1 -p 2222

Thursday, April 4, 2019

Simple bash to change /etc/resolv.conf

Here is a simple snippet to change the /etc/resolv.conf file on a Linux machine to be added into a shell script to change the DNS servers and other settings in the file. I went with this at the moment because the automation is not quite there and needed something quick to change the file on a small number of machines and will build more upon this in the coming days.

sudo su - <<HERE
rm /etc/resolv.conf &&
touch /etc/resolv.conf && \
printf "options timeout:2 attempts:5\n \
search dev.lab\n \
nameserver 172.0.0.100\n \
nameserver 172.0.0.101\n" >> /etc/resolv.conf
HERE

Wednesday, February 6, 2019

Schedule to Disable ESXi SSH

There's been something that continuously happens in the VMware environment that I manage where people will turn on SSH management of the ESXi hosts and leave it running. We could suppress the warning, or just go on living our lives paying no attention to it. But something that is more fun is to run a script to disable it on a schedule.

Now, this isn't anything new ... There are many posts on how to enable it and disable it with powercli. This is just something that looks at all the hosts in the environment then disables SSH only if it is enabled, and runs on an daily schedule

#############################
# Connect to vCenter        #
#############################

Import-Module VMware.VimAutomation.Core
$viserver = 'vcenter-server.domain.com'
$cred = Import-Clixml C:\scripts\creds\vc-credentials.xml

Connect-VIServer -Server $viServer -Credential $cred


#############################
# Disable Running SSH       #
#############################

Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" -and $_.Running -eq "True" } | Foreach {Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )} -Confirm:$false

Simple, now SSH won't be running unless it is necessary.