Jan302010

Change the color and format of the Bash prompt

Recently I got bored with the standard prompts and colors of the Bash shell in Linux, and decided to tweak them a bit.  What I decided to do was to make the root user account have a red prompt, mainly so I wouldn’t forget to log out once I was done and also to distinguish it from normal user accounts.

Editing the Bash Prompt

In order to change the color of the bash prompt, you need to edit the PS1 environment variable.  You can do this by typing the following in the bash shell:

export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m"


This will temporarily change the color of the bash prompt to red, and some other parts of the prompt as well.

Here is a basic breakdown of each part of the command:

The export PS1= is telling the shell that you are changing the PS1 variable

The \e[31;1m controls what foreground and background the prompt will have. 31 = Red foreground, 1 = black background.

The "[\u@\h \@ \W]#" means “your username”@”hostname” “localtime” “working directory”.

Making PS1 Environment Variable changes Permanent

If you want to make the changes permanent, you will have to append this line of code to the end of your “.bashrc” file in your home directory. You can do this with by editing the .bashrc file with any text editor in linux (nano, vi, vim, gedit) and manually add in the following line export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m" or this way:

1.  Put export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m" into a text file called "filenamehere"
2.  Then type the following command:

cat filenamehere >> ~.bashrc

3.  Then type exit in the terminal
4.  Open up a new terminal and you then should see the changes to the bash prompt.

This effectively uses cat to pint the export PS1 command to standard output in the shell, then with the append operator ">>" it will redirect the export PS1 command and add it to the end of the .bashrc file.  It's a fun and simple shell script! :)   CAUTION!!  Make sure you use the append operator ">>" and NOT the redirection ">" operator which will delete everything in the .bashrc file and replace it with what ever you redirected.

Reverse changes to PS1 Environment Variable
If you ever want to reverse the changes you have made to your PS1 environment variable, do the following:

1.  Open a new terminal window
2.  Type "vim .bashrc"
3.  Use the arrow keys on the keyboard to scroll down to where it has the "Export PS1=" line.
4.  Press the letter "d" twice on the keyboard to delete the whole line.
5.  Press escape
6.  press ":"
7.  after the colon, type "qw" for quit and write.  This will quit vim and save the file.
8.  Exit the terminal
9.  The next time you open a terminal for that user, you will have the default PS1 settings again.

If you would like to get more details and info about editing PS1 environment variables, visit the site in the references section below.  It has a wealth of info on the subject.  Enjoy!!

References:
http://www.funtoo.org/en/articles/linux/tips/prompt/

  • Share/Bookmark
Dec282009

Check if a certain user is logged on the Terminal Server using Powershell

Just recently I have been developing a Powershell script to determine if a specific user on a Windows Terminal Server is logged in or not. If the user happens to not be logged in, It will e-mail a message to me and another co-worker to log that user in immediately.

It basically uses the quser command to get a list of the currently logged in users on the terminal server and stores that info into the variable $nwt. I then setup a if statement to see if it matches “nwt” which is the username I want to make sure is logged in at all times. If it does not find the string nwt in the $nwt variable, it will then send an e-mail to myself and a co-worker notifying us that he is not logged in. This is possible via the “System.Net.Mail” .net object in the .net framework. Here is a site with more information on the System.Net.Mail object, that helped me add a CC address to the e-mail, here is the link: systemnetmail.com. And that’s really all there is too it!

So far the script has been a success, since it has alerted us as to when this critical user account logs out so we can quickly log him back in. (We have some software that needs this specific user account to run properly.. it’s really crappy software..)

Below is the code:
——————————————————————-

$nwt = invoke-expression -Command "quser nwt"

$date = date

if($nwt -is [Array]){

if($nwt[1] -match "nwt"){

write-host "--> NWT is logged in!"

echo "--> NWT is logged in! [ $date ]" > ./nwt.log

}

}else{

write-host "--> [ Problem ] NWT is not logged in!"

$emailFrom = "administrator@example.com"

$emailTo = "youremail@example.com"

$subject = "NWT is not logged in"

$body = "

The NWT user account is not logged in on Termserver. This was detected at:[ $date ]"$smtpServer = "mail.example.local"

$msg = new-object System.Net.Mail.MailMessage $emailFrom, $emailTo,

$subject, $body

$msg.CC.Add("co-worker@example.com")

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($msg)

}


——————————————————————-

  • Share/Bookmark
Dec112009

Windows – No Disk

The other day a client calls up and says she keeps getting a “Windows – No Disk” window, that will not go away no matter how many times she closes it.  I then asked when did this problem appear, in which she replied “after I pulled out the USB Flash Drive”.  So at this point I was thinking that some files she was working on, wanted to save themselves to the usb flash drive and could not since the drive was not present, thus causing the annoying “No Disk” error.  I then instructed her to save her work elsewhere on the hard drive and restart her workstation.  Windows - No Disk ErrorUnfortunately however, after she restarted her workstation and logged in the error continued to pop up!

Since the restart didn’t solve the issue, I knew it was time pull out my best tool in the shed “Google Search”.  After a couple google searches and a couple forums, I found out that the reason for this error is due to a usb driver that became corrupted during installation. The solution is to uninstall the usb drivers and reinstall them.

Once I had uninstalled all the usb drivers in Computer Management and restarted the workstation, the “Windows – No Disk” error appeared no more.  Mere mortals can only guess why the usb driver became corrupted in Windows, but thanks to google and a post on technet this XP annoyance was solved!

References:

http://social.technet.microsoft.com/Forums/en/itprovistahardware/thread/d5f38918-d11b-45c4-861d-b53aa3dbdaed

  • Share/Bookmark
Nov132009

NTBACKUP error – “End of Media encountered while backing up to non-removable media.”

Today I was investigating why some ntbackup jobs were failing on a remote server, and came across this error message in the logs : “End of Media encountered while backup up to non-removable media”. So I googled the error, and came up with a couple forums talking about how backups being saved to tape drives get this error when the tape runs out of space.  In my situation however, I was not using a tape drive for the backups, I was using an external hard drive.

Below is a screenshot of the NTBACKUP log

NTBACKUP log error

As you can see in the NTBACKUP log, when it tried to backup the files to the external hard drive it encountered the “End of Media” error.

So at this point I was hitting my head against the wall wondering why in the *(#$ is this happening!!?  The external hard drive had plenty of free space for the backups, it just didn’t make any sense!!  As a usual Sysadmin response I decided to dig dipper into the issue.  After thinking it though a bit, I had some inspiration to check “Computer Management” and take a look at the partition and filesystem setup of the external hard drive.  Below is a screenshot of what I discovered in Computer Management:

computer management - bestsrv

If you look volume E: in the top right, under filesystem it reads “fat32″! Once I saw that it occurred to me that the backups were failing because they exceeded the 4GB file size limit of the fat32 filesystem.  Sure enough after checking the backup sizes It was just barely over 4GB which would explain why NTBACKUP reported that the disk was out of space.  After discovering the true cause behind the failing backups, I backed-up all the data on the Western Digital MyBook, reformatted it with NTFS, and transferred all the data back onto the external drive.  That effectively solved the issue and allowed the customer to continue to backup the server without buying any additional hardware.

It just comes to show that most problems are simple to fix and easy to overlook.

References:

http://www.experts-exchange.com/Storage/Misc/Q_20710438.html

http://en.wikipedia.org/wiki/File_Allocation_Table

  • Share/Bookmark
Nov092009

How to prevent yourself from accidentally deleting files in Unix/Linux

Everyone at some point has faced that horrible moment when you realize that you just deleted a critical file, and start scrambling to find ways to recover it before it is too late.  If you haven’t faced this situation yet, you are the lucky few who have been spared from this horrible experience so far.  Just the other day, I found a really creative solution in Linux that will prevent you from accidentally deleting files by mistake.  It’s actually not that complicated either, and I am sure you will be using this technique to protect your most valuable files after your through reading my how-to.

Step 1 – Choose a Directory

HOWTO-prevent-accidental-delete

As you can see, I have created a directory called “test” and have created a ton of empty files and directories for testing purposes.  It was fun writing the Perl script to create all those “crap” files.

Step 2 – Create a empty file call “-i” in the chosen directory

HOWTO-prevent-accidental-delete-2

To do this simply use the touch command to create the empty file “-i”, as such: “touch ./-i”

Step 3 – Verify that “-i” exists in the current directory

HOWTO-prevent-accidental-delete-3.1

To make sure “-i” was created successfully, list the directory contents by invoking the “ls” command as show above.  Once you do that you will notice a new file called “-i” listed in the current directory.

Step 4 – Try to delete the files in the current directory

HOWTO-prevent-accidental-delete-3.9

Try deleting all of them at once by using “rm *”.

WARNING – make sure you have the “-i” the the directory before you try to remove any files!  Without the “-i” file there will be nothing to stop the rm command from removing your precious files!!

Below is a screenshot of what should happen when you try to delete any file in that directory.

HOWTO-prevent-accidental-delete-4

Did you notice what happened?  When we tried to delete any file in that directory, It would prompt us to confirm if we really wanted to delete each file.  This happens because the “-i” file is immediately interpreted by the Unix/Linux command line as a option for the “rm” command, which prompts before deleting a file.  If you man rm and look through the man page you will find the following entry:

  -i, --interactive
	      prompt before any removal

So that is the magic behind creating the empty “-i” file and how it helps prevent you from deleting file by mistake. Now don’t forget that if you use the rm -f command, it will delete the file no matter what!! That’s why it’s called the FORCE option!

Use this technique to your advantage and spread the word!

  • Share/Bookmark