Showing posts with label switch. Show all posts
Showing posts with label switch. Show all posts

Monday, February 26, 2018

Testing for multiple strings without much clutter using powershell

Yes, this hopefully will be quick, and yes it is powershell, which does not make me feel as dirty as if it was Windows. So hear me out.

I wrote a script a while ago that I needed to look for a pattern inside a string and then do something. In its simplest form, the code could look like this (second line is there to show the entire string):

$the_string = "There are pickles in a jar"
$the_string

if ($the_string -match "pickles")
{
        "Found me pickles"
}
else
{
        "nothing to declare"
}

If you wonder why I am using -match instead of -contains, there is a nice discussion you want to check. At least I learned a lot from it. When we run the script, which is henceforth called switchtest.ps1, we get

PS C:\Users\dalek> powershell -file .\dev\switchtest.ps1
There are pickles in a jar
Found me pickles
PS C:\Users\dalek>

So far so good. But, what if we want to do things based on whether other substrings are in the string? After all, instead of one single string we might be looping over a list of them, or reading a file and doing things based on what we find on a line-by-line-basis. We could add more if statements but that gets nasty quickly:

$the_string = "There are pickles in a jar"
$the_string

if ($the_string -match "pickles")
{
        "Found me pickles"
}

elseif ($the_string -match "there")
{
        "There is here"
}
else
{
        "nothing to declare"
}

Notes:

  • By default powershell is case insensitive; this is consistent with how DOS and Windows behaves, which is exactly the opposite of UNIX in general and Linux specifically.
  • If you have used other programming languages like python or C, you might remember switch statements (switch, then case-this and case-that). And, they are also implemented in powershell without the case word being explicitly used but the behavior is there for all to see.
  • -match looks for the substring anywhere in the string. So, it would find there in:
    • $the_string = "There are pickles in a jar"
    • $the_string = "Are there pickles in a jar?"
    • $the_string = "Pickles are in a jar over there"
Let's now redo switchtest.ps1 using case:
$the_string = "There are pickles in a jar"
$the_string

switch -wildcard ($the_string)
{
        "*pickles*" {"Found me pickles"}
        "there*" {"There is here"}
        default {"nothing to declare"}
}

and then run it

PS C:\Users\dalek> powershell -file .\dev\switchtest.ps1
There are pickles in a jar
Found me pickles
There is here
PS C:\Users\dalek>

The -wildcard option is where the main magic is. It allows me to enter a string as the search pattern, such as "pickles". The more astute of you probably noticed the asterisk (*) surrounding the search string. Without the first, it would only look for a string that starts with "pickles"; Since we only want to look for there when it begins the string we do not need the leading *. There is a thread in the Spiceworks forum showing another example of looking for strings that begin with a, in their case, specific letter using a case statement. The second one is so it will not look only for string ending with that substring.

There is nothing forcing each case statement to be a one-liner. In fact, it would be clearer to write the second one as

// Do something if the string begins with "there" 
        "there*" 
             {
                   "There is here"
             }

specially if we are going to do more than just write out a string.

Another thing you might also have noticed is the behaviour of the output does not match the one we did using the if statement. Reason is unless we specifically tell the switch statement to stop testing once it finds a match, it will keep going down to the list (the default is only done if nothing matches. Sometimes that is exactly what you want to do, but let's assume that is not the case. Just as in other languages, the command we need to give it is break. Let's add them

$the_string = "There are pickles in a jar"
$the_string

switch -wildcard ($the_string)
{
        "*pickles*" {"Found me pickles"; break}
        "there*" {"There is here"; break}
        default {"nothing to declare"}
}

and try it once more

PS C:\Users\dalek> powershell -file .\dev\switchtest.ps1
There are pickles in a jar
Found me pickles 
PS C:\Users\dalek>

Now the output looks just like the original script, but it is much cleaner and easier to expand.

Wednesday, September 14, 2016

Setting up atftp in (ubuntu) Linux

I have a switch whose firmware was in dire need of being updated. Thing is, the switch will only take the upgrade firmware if it is offered by a tftp server, don't ask me why. And that is something I tend not to have running; last time I used one was to network boot Solaris boxes. Sounds like an excuse to write another article.

Just to be different I will be deploying this on a Ubuntu Linux host instead of a CentOS/RedHat one as I usually do. Why? Doing the same all the time gets boring quickly. So, let's see which versions of tftpd we can pick and choose:

raub@desktop:~$ apt-cache search tftpd
tftpd-hpa - HPA's tftp server
atftpd - advanced TFTP server
libnet-tftpd-perl - Perl extension for Trivial File Transfer Protocol Server
tftpd - Trivial file transfer protocol server
uec-provisioning-tftpd - the UEC Provisioning TFTP server
raub@desktop:~$

After careful scientific consideration, the atftpd one sounds more interesting (Multi-threaded TFTP server implementing extension and multicast), so we will pick that one. I think this is the part of the show in which we go through the steps to do the deed:

  1. A good place to start is to install it. I like command line and apt-get, so I think

    sudo apt-get install atftpd

    should do the trick. Of course you can use aptitude or the GUI. But I am lazy.

  2. Traditionally the directory used to put stuff that will be shared by tftp is /tftpboot, but current practice is to use /srv/tftp. In fact, atftpd does create /srv/tftp for you. For the same of showing how to customize things, let's say we want to be old school. And that means creating /tftpboot ourselves:

    sudo mkdir /tftpboot
    sudo chmod -R 777 /tftpboot
    sudo chown -R nobody:nogroup /tftpboot
  3. This might be a good time to put the files we want to be shared, in this example a file called image.bin in /tftpboot

  4. We need to configure it by editing /etc/default/atftpd. Here is what mine looks like

    raub@desktop:~$ cat /etc/default/atftpd
    USE_INETD=false
    OPTIONS="--tftpd-timeout 300 --retry-timeout 5 --port=69 --mcast-port 1758 
    --mcast-addr 192.168.0.0-255 --mcast-ttl 1 --maxthread 100 --verbose=7 /tftpboot"
    raub@desktop:~$

    where:

    • --port=69 we are forcing it to use the default tftp port
    • --mcast-addr 192.168.0.0-255 specifies the multicast address range we will be using. Being lazy, I am using the entire 192.168.0.0/24 range
    • /tftpboot is the directory we will be sharing as explained above. By default the config file specifies /srv/tftp which means if we put our file in /tftpboot we would get a message like

      Sep 13 13:54:00 desktop atftpd[28045]: File /srv/tftp/image.bin 
      not found

      when we try to fetch the file

    • --verbose=7 is the highest amount of verbose we can use. By default its value is set to 5.

    Once it starts properly (service atftpd start should be enough to start it), you should see something like

    raub@desktop:~$ ps -ef|grep ftp
    raub     16510 11504  0 15:26 pts/11   00:00:00 grep ftp
    nobody   28161     1  0 Sep13 ?        00:00:00 /usr/sbin/atftpd --daemon --tftpd-timeout 300 --retry-timeout 5 --port=69 --mcast-port 1758 --mcast-addr 192.168.0.0-255 --mcast-ttl 1 --maxthread 100 --verbose=7 /tftpboot
    raub@desktop:~$

How to get the file using tftp is beyond this discussion because it depends on your OS and the tftp client you are using. For instance the switch might show a webpage where you can configure the name of the tftp server -- 192.168.0.102 in my example -- and the name of the file you want to grab. What is more interesting is to see how the entire enchilada from we started the tftp server until we get the file image.bin looks like. By default (can be configured) we would see that in /var/log/syslog:

Sep 13 13:55:52 desktop systemd[1]: Starting LSB: Launch atftpd server...
Sep 13 13:55:52 desktop atftpd[28160]: Advanced Trivial FTP server started (0.7)
Sep 13 13:55:52 desktop atftpd[28153]: Starting Advanced TFTP server: atftpd.
Sep 13 13:55:52 desktop atftpd[28161]:   running in daemon mode on port 69
Sep 13 13:55:52 desktop atftpd[28161]:   logging level: 7
Sep 13 13:55:52 desktop atftpd[28161]:   directory: /tftpboot/
Sep 13 13:55:52 desktop atftpd[28161]:   user: nobody.nogroup
Sep 13 13:55:52 desktop atftpd[28161]:   log file: syslog
Sep 13 13:55:52 desktop atftpd[28161]:   not forcing to listen on local interfaces.
Sep 13 13:55:52 desktop atftpd[28161]:   server timeout: Not used
Sep 13 13:55:52 desktop atftpd[28161]:   tftp retry timeout: 5
Sep 13 13:55:52 desktop atftpd[28161]:   maximum number of thread: 100
Sep 13 13:55:52 desktop atftpd[28161]:   option timeout:   enabled
Sep 13 13:55:52 desktop atftpd[28161]:   option tzise:     enabled
Sep 13 13:55:52 desktop atftpd[28161]:   option blksize:   enabled
Sep 13 13:55:52 desktop atftpd[28161]:   option multicast: enabled
Sep 13 13:55:52 desktop atftpd[28161]:      address range: 192.168.0.0-255
Sep 13 13:55:52 desktop atftpd[28161]:      port range:    1758
Sep 13 13:55:52 desktop systemd[1]: Started LSB: Launch atftpd server.
Sep 13 13:55:59 desktop atftpd[28161]: socket may listen on any address, including broadcast
Sep 13 13:55:59 desktop atftpd[28161]: Creating new socket: 192.168.0.102:45115
Sep 13 13:55:59 desktop atftpd[28161]: Serving image.bin to 192.168.0.3:2295
Sep 13 13:56:03 desktop atftpd[28161]: End of transfer
Sep 13 13:56:03 desktop atftpd[28161]: Server thread exiting

The underline on the entry when the file image.bin is transferred was added to make it easy to see. And that is pretty much all I had to do. Once file was transfered, I stopped atftpd and then remove it

sudo apt-get remove --purge atftpd

because I do not like to have unused services running.

PS: Always backup your switch/network device's config before upgrading its firmware in case it reverts to default as part of the upgrade process. Guess who forgot to do that? Good thing I had good notes and could reconfigure it using the time-honored cut-n-paste technique

Thursday, August 06, 2009

Disassembling a Brocade 2800 fabric switch (and finding its serial port/resetting its password in the process!)

What we have here is a Brocade 2800 fabric switch, but you probably new that by reading the title of today's chapter. In previous jobs I have worked with 24000 and 48000 Brocade switches like this hairball here:

Yes, they are bigger (256 fibre ports for the 24000 and 512 for the 48000 vs 16 for the 2800) and faster (up to 4GB compared to 1GB) but they all do the same thing: they were originally designed to be used in storage, connecting SAN and NAS devices to the hosts that use them. The fabric part of the name means just that. But, we are here talking about the 2800, so sit down and take notes because I will not repeat myself.

As mentioned before, the 2800 has 16 fibre ports... and one ethernet port. The ethernet port, a 100Base-TX one, is normally used as your console: you assign an IP to it, connect to the switch either using a web interface or ssh, and do some switch managing.

Tearing it apart.

All that talk about what these switches can do is most interesting, but that is not what we are here to do today. We want to see what is inside the box! So, let's get busy!

The first thing we should do is to remove the two redundant power supplies. To do so, you first pop the clip that goes around the power connector; you will note it becomes a neat handle (shown here is the left power supply sitting on the top of the right, which is still in the switch).

Then, you press down on the tab the clip hinges around. That unlocks the power supply from the case, allowing you to slide it out from the front.

Here is another view of the hole the right power supply hides. Note the tabs on the left. They are what hold the top in place, which we will be removing in the next step.

Ok, I was lying. We are not removing the top of the switch just yet. First, we need to remove the plastic cover that surrounds the front LCD and the buttons that control the menu on the display. It will come off if you grab it on one side and work it out, I promise! Just do not be too aggressive.

Once the cover is off, well, the cover is of. Now we can go back to the business of removing the top cover.

This next step require a bit of force and traction: you need to slide the lid forward which will mean you (or a friend) will have to hold the switch in place while wiggling the cover and slowly moving it. I did that by myself but will say finding a place to hold the cover was tricky. This is in my opinion the hardest step in this entire adventure.

With a judicious use of curse works, strength, and even finesse, it should eventually slide forward until it can't go any further. Yeah, as the picture below shows it is not much, but remember all it has to do is to line its tabs with the holes you saw through the power supply bays. Now, you have to grab the back of the cover and tilt it up around the front. Then, a bit of wiggling should get the cover out.

Here is how the switch looks like topless. Those wires on the top come from the front panel. You can also see the fans on the rear. Also note the titled plate those wires go through. It is probably that way to help direct the air from the motherboard to the fans; we will talk about them in a moment.

And here is a rear view of the case. Can you see the motherboard hiding there?

I promised I was going to talk a bit about the fans, and talk about them I will. If you look on the back of the case, you will find 4 thumbscrews that look like this:

They hold the fan assembly to the case. So, if you unscrew them, you can remove the fans without much fuss.

Overall I will say this is very easy switch to take apart, which means you can replace any of its components in minutes.

Hunting for the serial port.

If you are like me, you will make sure you keep all documentation on the switch somewhere safe... or safer than piling up by the trash bin. The more confidential stuff, like its configuration and passwords, you probably have a secure encrypted file somewhere with them; it sure beats post-it notes! But, what do you do if you lose your password or the previous guy left without letting anybody know it? Well, two things you can do: call Brocade and have them send you a file to reset the password (which they will gladly do depending on your contract) or you have to take matters on your own hand. Brocade was actually nice: in addition to the normal ethernet console port, they hid a serial port inside the switch. As I looked online for info on it, it seemed that its location was only told to the initiated after they proven their mastery of the secret network society handshake.

Jokes apart, I have never found pictures of the inside of this switch, much less detailed information of the location of the serial port (I guess it rather be called IDC but I will ignore its wishes). And this is why I decided to do this entire writeup. The part about tearing the siwtch apart is more of a bonus feature; this is the neat stuff:

Getting to the serial port will require one tool and one tool only: a flathead screwdriver. If you look on the front of the switch, you will find a slotted cylinder thingie about in the middle of the ports; that is where you put the screwdriver on:

That actually turns a long shaft which ends inside the case. The threaded end of the shaft turns around a nut that is attached to a bracket. Just click on the pict below to see it; it is kinda behind where the wires from the front panel go down.

As you unscrew it, the drawer containing the motherboard and the switch ports will slide forward. It will not go very far; you will know when you unscrewed it as far as it can go. After that you will need to work it out. It is tight in there. I wiggled it and eventually it slid enough I could grab it and pull it out like a drawer:

Here is what the motherboard looks like once it is removed from the case. Now, what about the serial port you talked about? Calm down and look to its upper left corner, by the left large white connector. Can't see it? Let me zoom a bit.

How about now? Can you see it? Hint: I circled it in yellow.

And here is yet an even closer look. Can you see the pin numbers? Hint: White dot usually indicates pin 1.

If you want to make your serial cable, here is the pinout:

I will add how to reset the password later.