Quick System Info – BASH

Here is a BASH script to get basic system inventory information about your computer and parse it into CSV format.

#!/bin/bash
FIELDS="\"ComputerName\",\"Manufacturer\","
FIELDS+="\"Model\",\"ServiceTag\",\"Memory\","
MYHOSTNAME="$(hostname)"
MANUFACTURER="$(dmidecode -s system-manufacturer)"
MODEL="$(dmidecode -s system-product-name)"
SERVICETAG="$(dmidecode -s system-serial-number)"
MEMORY="$(free |grep "Mem:" |awk '{print $2}')"
MYINFO=\""${MYHOSTNAME}"\",\""${MANUFACTURER}"\",
MYINFO+=\""${MODEL}"\",\""${SERVICETAG}"\",
MYINFO+=\""${MEMORY}"\",
echo "${FIELDS}"$'\n'"${MYINFO}"

Troubleshooting Network Connectivity Issues

This tutorial will walk you through the steps of tracking down layer 1 & 2 connectivity issues on a Ethernet segment.

Scenario: Your connection to a device intermittently drops, or is painfully slow.

This could be the result of any number of factors, such as:

  • Bad Medium (cabling, etc.)
  • Electrical Interference
  • Bad Driver
  • Bad Device
  • Mis-configured Device

So let’s start at the top of the list.

Bad cabling is everywhere, I’ve come into an organization just two years after hundreds of new cables were pulled, to find 30% of them were bad, with 8% needing to be pulled again.

So how can we verify if our problem is cabling.

The first step is to verify that we are using the correct cable(s) and distances for the speed of our Ethernet Network. The University of Wisconsin has a great chart here:

The second step is to replace the cable(s), with a known good unit.

Step two doesn’t work very well on cabling that is run through the walls, ceilings, and floors, so you’ll need an analyzer for that purpose. Cable analyzers are expensive, and you’ll probably want to rent one or call in a professional low voltage wiring contractor to run the test for you.

Are you having electrical interference?
Try to see that your cables are not in close proximity to electrical motors, transformers, fluorescent lamps, etc.

Do you have a Bad Driver? Check the NIC’s hardware compatibility for your OS.

To determine your device in linux:
Use “ifconfig” to determine the device name (usually “eth0″)
Use “dmesg |grep DeviceName” to determine the device type

To determine your device in BSD:
Use “ifconfig” to determine the device name
Use “grep DeviceName /var/run/dmesg.boot

Check your compatibility at the sites below:
Linux
BSD
VMware
Windows

Do you have a bad device? Try a different port/NIC or completely different computer/switch/hub or other device if you have one available.

Do you have a mis-configured Device?

The first item to examine is our Duplex status. Are both ends set to the same speed and duplex? Are we seeing a lot (more than 0.5%) of packets being dropped?

For linux/bsd try “ifconfig” from the command line. Using “netstat -i” gives us a succinct look at our network statistics.

For linux, using “ethtool -S eth0” gives us a wealth of detailed information about “eth0″. You can also try “ip -s link“, “nstat“, and “sar -n DEV 1 3” if you have sysstat installed.

Our next step will be to whip out trusty old “tcpdump” or the graphical “wireshark” and take a look at the packets flying over the link, typically mirroring a port on our switch. How to use those tools will be a discussion for a future day.

Removable EXTx drive for non-root Linux user

Today, we will cover setting up an external removable drive using the EXT3 file system, and to allow us to mount the drive as a normal (non-root) user. This allows us to keep existing Linux file permissions when we write to the drive, and features journaling which ensures that what we write to the removable drive, is completely written to disk.

This system also allows us to easily create separate mount points for distinct removable drives, which facilitates the use of multiple drives in a scripted backup strategy.

So let’s get started!

First thing we do is determine how the drive is recognized by the kernel:

From the command prompt:

sudo tail -f /var/log/messages
or
su -c tail -f /var/log/messages

and look for something on the order of “[sdb] Attached SCSI disk” after we plug in our removeable device.

The kernel output will tell us if there are any existing partitions (look for “sdb1″, “sdc1″ etc.)

Ctrl-C to exit tail

Next we need to create a partition table, if there are existing partitions you’ll need to ensure thay do not contain important data before you remove or overwrite them.

Again from the command prompt:

sudo fdisk /dev/sdb
or
su -c fdisk /dev/sdb

Choose “n” for “new partition”

Then “p” for “primary partition”

Then “1” for the first partition.

Then the defaults for the sectors.

Then “w” to write changes and quit.

Our next step is to create a filesystem

Again from the command prompt:

sudo mkfs.ext3 /dev/sdb1
or
su -c mkfs.ext3 /dev/sdb1

This might take a while depending on the drive size

While we wait, open another terminal window and continue working.

We are going to name our disk “BU_Linux” but feel free to change the name, but avoid spaces and wildcard/reserved characters in the name (No: “.”, “\”, “?”, “!”, etc.). We will mount the disk using the same name under the “/media” directory.

Again from the command prompt:

sudo mkdir -p /media/BU_Linux
or
su -c mkdir -p /media/BU_Linux

Next we need to backup “/etc/fstab”

sudo cp /etc/fstab /etc/fstab.org
or
su -c cp /etc/fstab /etc/fstab.org

Then we can edit “etc/fstab” using vi:

sudo vi /etc/fstab
or
su -c /etc/fstab

and add a line as below:

"LABEL=BU_Linux  /media/BU_Linux  ext3  defaults,user,noauto  0 0"

We then will go back to our prior console window, hopefully mkfs.ext3 has finished.

We now create the disk label using “e2label” which can display or change the filesystem label on the ext2, ext3, or ext4 filesystems.

Again from the command prompt:

sudo e2label /dev/sdb1 BU_Linux
or
su -c e2label /dev/sdb1 BU_Linux

Now let’s mount the drive!

Again from the command prompt:

mount -L BU_Linux

Let’s check if the drive is mounted.

Again from the command prompt:

mount |grep BU_Linux

Which should return:

“/dev/sdb1 on /media/BU_Linux type ext3 (rw,noexec,nosuid,nodev,user=MyUserName)”

Now let’s examine the drive.

Again from the command prompt:

df -h /media/BU_Linux

Which tells us how much free space is available

and

ls -al /media/BU_Linux

which should show us the “lost+found” directory

Now if we try to write a file ie.:

echo "This is a test" > /media/BU_Linux/test.txt

You’ll see that you cannot write to the disk.

So let’s fix this!

Again from the command prompt:

sudo chmod 777 /media/BU_Linux
or
su -c chmod 777 /media/BU_Linux

And try again!

echo "This is a new test" > /media/BU_Linux/new_test.txt

And we should have success.

This change will survive remounts because the permission you just changed is resident on the volume’s filesystem.

When you are finished you’ll need to unmount the file system before removing the drive.

Again from the command prompt:

umount /dev/sdb1

Removing Windows Clients from a non-existent domain

In this post we discuss removing windows clients (xp, etc.) from a domain that no longer exists, or which you no longer have access.

In the course of migrating a small group of computers to a new Linux server running file and print services, I realized that I had to first remove the client machines from a windows domain that no longer existed due to catastrophic hardware failure.

While I had local administrative access, I could not change the clients to workgroup membership, as this step requires authenticating to the domain as an domain administrator.

What to do: Recreate the domain temporarily, and remove the clients from the domain.

On the server side:

  • Navigate over to the “Technet Evaluation Center” and download an evaluation copy of Microsoft Server. You’re going to need to be patient as the download is a few gigabytes.
  • Load the server operating system on your hardware/VM
  • Install the “Dot Net” feature
  • Download updates
  • Configure a static address, setting the machine’s IP address as it’s DNS server’s address
  • Remove internet access from the attached local network
  • Add the “Domain Services” role
  • Run “dcpromo” in an administrative command shell, you’ll need to use the same domain naming schema as the missing domain
  • Create a new user and add them to the domain administrator’s group

On the client side:

  • Set a static IP address to be on the same subnet as the server. You’ll want to set IP address for the default gateway and DNS server to be the address of the newly created domain controller.
  • Navigate to system properties – computer name, then click change
  • Set it to workgroup mode, it will then ask you for the credentials of a domain administrator
  • Give the the credentials of the domain administrator that you created on the temporary domain controller
  • Reboot the client machine


That’s all there is to removing the client from domain membership.

Learning the BASH shell

Today we’ll explore the BASH shell a bit.

BASH is extremely powerful, yet succinct.

To view the built-in BASH commands available on a Linux system try:
“man -k bash”

To save these to a file in your home directory for later review try:
“man -k bash |awk ‘{print $1}’ > ~/bash_commands_available.txt”

And to view the available options for each command try:
“help command_I_want_to_learn”
replacing “command_I_want_to_learn” with something like “cat”

To learn about the help system try:
“help help”

Timestamp your bash history

In Memoriam: Dennis Ritchie

I want to begin this blog with something incredibly useful, yet simple.

When you find yourself trying to remember when you made that all important, late night configuration change, it really helps to have timestamps in your bash history.

Try adding the lines below to your “.bashrc”

       HISTTIMEFORMAT=’%F %T’
       export HISTTIMEFORMAT