Monthly Archives: January 2013

Using Find-String to grep in Powershell

For the longest time I have not been a fan of embracing the shell in the Windows world, but more and more I find that Powershell is able to do the things that I need.  I suppose my seething hatred was in part due to my negative bias towards Windows and the lack of useful tools from the command line in Windows.  Increasingly lately, I have been changing the way I think about and utilize the command line in Windows with Powershell.  And to be honest, I’m really beginning to fall in love with Powershell the more I get work with it and the more I get to see how to apply it in Windows based environments.  The good news is that Microsoft has put a lot of effort into this and are adding improvements and features all the time.  It still has a long way to go, but I can already see this as an alternative to GUI based administration in the Windows world and finally begin to feel like I can see the promise land on the horizon.  Using Powershell has been somewhat of a paradigm shift (in my opinion anyways) to how to do Windows administration recently and I feel like it will only get stronger and more common in the years to come.  So in this post I will try to show you some of the flexibility as well as some of the power that Powershell has to offer with some great tools from the community.

To highlight what I am talking about, let’s talk about grep, a well known and loved tool in the *nix universe.  I love grep.  For the longest time, I hadn’t known of a way to grep in the Windows world until just recently with a wonderful third party Powershell module called Find-String.  There are a few commands that you need to get this working.  To start, we need to get a module installed that is basically used for package management.  This tool is called PsGet. Installation is super simple, just run the following from a Powershell prompt, and ensure that your execution policy is set at least at remotesigned (Set-ExecutionPolicy RemoteSigned if you don’t have this turned on).

(new-object Net.WebClient).DownloadString(“http://psget.net/GetPsGet.ps1“) | iex

This should go out, download and install PsGet for you.  Once all that has completed you should be able to install Powershell modules that are contained in the PsGet repository.  With this installed we can just reach out to the PsGet repo and install our module.

Install-Module Find-String

Easy, simple, and clean.  That is the best part, there isn’t a ton of leg work to get this stuff working correctly and is why I’m enjoying Powershell so much these days.  Now we have a very functional grep clone!  Usage is quite a bit similar to grep, fore help you can do a Get-Help Find-String or look at the link I posted earlier to the author’s github page.  If you want to look for a word or substring in a file just use something like this:

Find-String word example.txt

This will output all occurrences of the word you are looking for in the file example.txt.  Here is a screen shot to show you.

Find-String in action

What other cool Powershell stuff are you doing?  I would love to hear about other cool uses that can be beneficial in every day use.

More tips for improvement

The previous post I wrote about becoming a better sysadmin, covering general points and tips for self improvement turned out to be more popular than I thought it would be (okay not really, but for me it was at least!).  So I decided to write a little bit of a follow up post in regards to general improvement and subsequently have decided to focus more of my time and effort on including more posts related to that type of content.  I find the topic of self improvement to be interesting and would love to write more about my findings, so I will be experimenting a little bit with these less technical pieces a little bit more I think. I would also like to hear what others have to say about these posts and the topic of self improvement so let me know.

While this post is primarily focused on self improvement this can easily be adopted to anybody in the IT industry that is just looking for a way to improve themselves and get better.  So while the hard skills (certifications, books, blogs, anything that specifically relates to a specific area) are incredibly important, why not throw in these general and well known strategies to help with your improvement?  I think the positive benefits will heavily outweigh the negatives in this scenario.

General Tips for improvement

These simple tips can go a really long way.  I read threads all the time about how to get better and how to improve mental capacity, blah blah blah and the following suggestions always seem to pop up.  What I have found to be true is that there is no magical instant way to improve yourself, I am learning that the hard way.  To me, the best way to see results and really work on yourself starts by changing your habits, working hard and being consistent.  That might not be what you are looking for, but trust me, these small tips can go a long way in becoming better at what you do.

Exercise - Time and again I hear and read about the massive benefits of proper exercise.  I did not take this advice seriously until just recently and can say that it has made a huge difference in the way I think and the way I feel.  I used to always feel beat down and terrible after work until I started exercising so those times of the day where you feel a lot slower are lessened.  I wouldn’t recommend starting out by completely changing the way you live your life.  Something simple to start with.  This summer I started running again, I made a routine out of going out for a run after work, and just kept at it until I started seeing changes.  I gradually increased the period of time and distance of my runs, then I added in weight lifting and other types of exercises gradually. By no means am I a hardcore athlete now but I do believe in the importance and benefits of exercising and working your body regularly to improve your mind.

Sleep - This is probably the most important thing to remember when you are trying to work on hacking your mind and improving yourself.  8 hours of sleep seems to be the general rule of thumb, and it should not be overlooked when you are evaluating yourself and your goals for getting to where you want to be.  If you want to wake up early, you need to go to sleep early, it really is as simple as that.  It is also important to be consistent on your sleep schedule so your body can get used to when it should slow down and when it should speed up (even on weekends!).  For example, getting in a routine of winding down at a certain time, say 9 pm  every night by reading a book for an hour to train your body that it is time to sleep.  Read until say 10 pm every night if you want to wake up at 6 am to get the sleep consistency your body needs, also giving your body enough time to repair and heal itself to get up and going.

Diet - Also important.  I realize that everybody is different and I don’t want to speak as if an authority on the subject so please take this advice at face value.  The point I want to make though is that diet isvery important in improvement.  Again, I do not want to encourage anybody to go all out and completely change every eating habit they have at once.  You will crash and burn like many others, it make work for some but generally you will be safer and more likely to make an impact if you take things slowly.  Work on one thing at a time and gradually make the changes to improve your diet and health.  As an example, start by cutting out something small, like cutting out a particular type of food that isn’t exactly healthy.  For me it was soda, and once I had that under control I was able to cut out (for the most part) fast food, etc.  Not entirely, I wouldn’t advocate that but cutting back is a good first step.  Basically doing something is better than doing nothing.

Bonding network cards: or “How I learned to stop worrying and love 2Gb/s ethernet”.

Most servers ship with at least two network ports, often more.

And yet so often we plug one of them into a switch and ignore the other one. We’ve paid for an expensive server with very capable networking, and now we’re going to ignore half of its capabilities. Meanwhile, we’re asking it to do more and more. Sooner or later, that gigabit network port is the bottleneck.

Why not use both network ports simultaneously?

There’s various ways you can set this up. Some require special configuration of managed switches; some don’t. For this blog post, I’m going to concentrate on methods that don’t require special switch configuration because they’re a little bit easier and they’re somewhat less fragile – you don’t risk your network collapsing in a big heap just because someone plugged a network port into the wrong socket. (We also get the added bonus that if one of the two network ports in our server fails, it’ll still work, albeit more slowly. But I can’t remember the last time I saw a network port fail…)

These instructions are written purely for Debian Squeeze. You may need minor tweaks to use them in Ubuntu; you’ll almost certainly need significant changes to use them in other distributions.

First, install the ifenslave package:

apt-get install ifenslave

Configuration is just a few lines in /etc/network/interfaces:

# The primary network interface
auto bond0
iface bond0 inet static
    address 192.168.42.43
    broadcast 192.168.42.255
    netmask 255.255.255.0
    gateway 192.168.42.254
    network 192.168.42.0
    bond_mode balance-tlb
    bond_miimon 100
    bond_downdelay 200
    bond_updelay 200
    slaves eth0 eth1

Whatever you do, make sure you test everything on a non-production network first!

More information: Linux Kernel bonding documentation

Protip January: Get your external IP from the command line

Ever need to grab your IP quick but don’t want to get out of the command line or stop whatever you’re working on?  Or how about if you have SSH’d into a number of different servers and you simply want to know where you are at currently?  This little trick enables you to quickly determine your public IP address without leaving the command line.

I’ll admit, I didn’t originally come up with this one, but liked it so much that I decided to write a quick post about it because I thought it was so nice and useful. There is a great website called commandlinefu.com where users can post all their slick one liners, which is where I found this one.  If you haven’t been there before I highly recommend it, there is some really good stuff over there.

This one is simple yet quite useful, which is what I’m all about.  The command uses curl, so if you don’t have that bad boy installed yet you’ll need to go get that quick (Debian based distros).

sudo aptitude install curl

Once that is installed simply run the following:

curl ifconfig.me

And bam!  Emeril style.  Let that go out and do its thing and you will quickly have your external IP address.  I like this method a lot more than having to jump out of the shell and open up a browser then going to a website to get this information.  It might not save that much time but to me just knowing how to do this is useful and knowledge is power.  Or something.