Thursday, August 25, 2011

Get OS Version through Visual Basic Scripting

Just create a new document on your desktop and rename it to GetOS.vbs

Right click and edit, paste the code below and save it. Double click and run it to display your Windows Version. Good code to use in batch or other functions.


msgbox GetOS

Function GetOS()
'Requires WSH 5.6
'CMD window will flash as there is no way to supress it when using exec.
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim strVer : set strVer = WshShell.exec("%comspec% /c ver")
Dim sResults
sResults = strVer.stdout.readall

Select Case True
'Add more info to the 98 and 95 to get the specific version. i.e. 98SE 95 a,b,or c
Case InStr(sResults, "Windows 95") > 1 : GetOS = "W95"
Case InStr(sResults, "Windows 98") > 1 : GetOS = "W98"
Case InStr(sResults, "Windows Millennium") > 1 : GetOS = "WME"
Case InStr(sResults, "Windows NT") > 1 : GetOS = "NT4"
Case InStr(sResults, "Windows 2000") > 1 : GetOS = "W2K"
Case InStr(sResults, "Windows XP") > 1 : GetOS = "WXP"
Case InStr(sResults, "Version 6.0") > 1 : GetOS = "Vista"
Case InStr(sResults, "Version 6.1") > 1 : GetOS = "W7"
Case Else : GetOS = sResults
End Select
End Function

Friday, August 12, 2011

Migrate users from one Linux machine to another

Have you ever had a need to migrate current running Linux users from installation to another? That would be a simple task if the user count was low. But what happens when the user count is in the hundreds? What do you do then? If you’re not using LDAP, you know you will have to migrate the users’ data, passwords, etc from the old machine to the new. Believe it or not, this is just a matter of a few commands – not necessarily simple commands, but it’s not as complex as you would think.

In this article I am going to show you how to make this migration so your Linux users do not loose their data and their passwords are all retained.

What we migrating

The list is fairly simple:

  • /etc/passwd - Contains information about the user.
  • /etc/shadow - Contains the encrypted passwords.
  • /etc/group - Contains group information.
  • /etc/gshadow - Contains group encrypted passwords.
  • /var/spool/mail - Contains users email (the location will depend upon the mail server you use).
  • /home/ - Contains users data.

Unfortunately these files can not simply be copied from one machine to another – that would be too easy. Just make sure you enter the following commands correctly.

Source machine

These are the commands you will need to run on the machine you are migrating users FROM. I will assume you are doing this on a system that uses a root user (such as Fedora), so all commands will be done as root:

mkdir ~/MOVE

The above command creates a directory to house all of the files to be moved.

export UGIDLIMIT=500

The above command sets the UID filter limit to 500. NOTE: This value will be dictated by your distribution. If you use Red Hat Enterprise Linux, CentOS, or Fedora this value is shown in the command above. If you use Debian or Ubuntu that limit is 1000 (not 500).

awk -v LIMIT=$UGIDLIMIT -F: ‘($3>=LIMIT) && ($3!=65534)’ /etc/passwd > ~/MOVE/passwd.mig

The above command copies only user accounts from /etc/passwd (using awk allows us to ignore system accounts.)

awk -v LIMIT=$UGIDLIMIT -F: ‘($3>=LIMIT) && ($3!=65534)’ /etc/group > ~/MOVE/group.mig

The above command copies the /etc/group file.

awk -v LIMIT=$UGIDLIMIT -F: ‘($3>=LIMIT) && ($3!=65534) {print $1}’ /etc/passwd | tee – |egrep -f – /etc/shadow > ~/MOVE/shadow.mig

The above command copies the /etc/shadow file.

cp /etc/gshadow ~/MOVE/gshadow.mig

The above command copies the /etc/gshadow file.

tar -zcvpf ~/MOVE/home.tar.gz /home

The above command archives /home.

tar -zcvpf ~/MOVE/mail.tar.gz /var/spool/mail

The above command archives the mail directory. NOTE: If you are using Sendmail this is the correct directory. If you are using Postfix that directory most likely will be /etc/postfix.

Now it’s time to move everything in ~/MOVE over to the new server. You can do this using the scpcommand like so:

scp -r ~/MOVE/* USER@IP_OF_NEW_SERVER:/home/USER/

Where USER is the username you will use to send the file and IP_OF_NEW_SERVER is the address of thenew server. NOTE: If this server is not on line yet you can always copy these files onto a thumb driveand move them that way.

Target machine

Now we’re working on the new server. Follow these commands (run as the root user):

mkdir ~/newsusers.bak

The above command will create a new directory that will house the backup of the current users.

cp /etc/passwd /etc/shadow /etc/group /etc/gshadow ~/newsusers.bak

The above command will copy the necessary files to the new backup directory.

cd /PATH/TO/DIRECTORY
cat passwd.mig >> /etc/passwd
cat group.mig >> /etc/group
cat shadow.mig >> /etc/shadow
/bin/cp gshadow.mig /etc/gshadow

The above commands will restore all password files onto the new system. NOTE: Where /PATH/TO/DIRECTORY is the location where you copied the files onto the new system.

cd /
tar -zxvf /PATH/TO/DIRECTORY/home.tar.gz

The above commands will first change you to the / directory and then unpack the archived /homedirectory. NOTE: Where /PATH/TO/DIRECTORY is the location where you copied the files onto the new system.

cd /
tar -zxvf /PATH/TO/DIRECTORY/mail.tar.gz

The above commands will first change you to the / directory and then unpack the archived/var/spool/mail directory. NOTE: Where /PATH/TO/DIRECTORY is the location where you copied the files onto the new system.

You can now reboot your system with the users in place.

Screensavers starting randomly after upgrade to OS X Lion

The Screen Saver options in OS X Lion are no different from those offered in Snow Leopard, and like Snow Leopard are available to configure in the Desktop & Screen Saver system preferences. As a result, you should expect the screensavers to behave as they did in Snow Leopard; however, a few people are noticing after upgrading to Lion that their screensavers seem to be randomly activating.


If this problem is happening to you, first try adjusting some of your Screen Saver settings by either changing the screensaver, adjusting its activation time, or setting various hot corners that you may have previously used for activating or inactivating the screensaver. If this does not work then the next step would be to remove the screensaver preferences file, in case it is corrupted or otherwise contains settings that are incompatible with Lion.


To remove the Screen Saver preferences, press the Option key and select Library from the Finder's Go menu. In the library, go to the /Preferences/ByHost/ folder and delete the file called "com.apple.screensaver.NUMBER.plist," where NUMBER is a string of alphanumeric characters that will be unique to your computer.


After the preferences have been deleted, go back to your Screen Saver system preferences and configure your screensavers again.