Python - while else statement

14. August 2012 07:56


In python most people are familure with a combination of if / else or a while loop. Did you know you can combine a while with an else statement. the obvious main advantage here is to prevent using extra variables and nested statement which makes the code shorter and clearer to understand.


This short example demonstrates this functionality.



lst = []
while lst:
    print "There is a list"
    print "Empty List"



In the case above because the contents of the while loop are no executed because the list is empty it will print "Empty List" instead. This can also work with for loops like in the following example.



for x in lst:
    if x == "Something":
    print "Not Found"


You should probably take notice of the break statements in the above code as the else statements runs on the last evaluation of the loop condition so when a loop terminates because its condition is false the else will always run. The break statement changes this so that the loop conditition is true and the else will be skipped.

E-mail Kick it! DZone it! Permalink

C++ - Check an IP Address is in a IP/Mask range

9. August 2012 08:00


This is a short guide on how to check if an ip address is inside a certain ip / mask combination. An example would be to ensure that an ip address like "" is inside ""


One of the problems that you will face is that the standard functions for converting ip addresses to an unsigned int typically will also convert the address to network byte order which will not make. You could use the ntohl function to convert it back or a simple parsing functions like below to convert it to an unsigned long using sscanf.


uint32_t IPToUInt(const std::string ip) {
    int a, b, c, d;
    uint32_t addr = 0;

    if (sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
        return 0;

    addr = a << 24;
    addr |= b << 16;
    addr |= c << 8;
    addr |= d;
    return addr;


The rest is the very stright forward since an ip address is really just a 32bit number if you convert the ip address the subnet and the mask a stright check can be performed like this.


bool IsIPInRange(const std::string ip, const std::string network, const std::string mask) {
    uint32_t ip_addr = IPToUInt(ip);
    uint32_t network_addr = IPToUInt(network);
    uint32_t mask_addr = IPToUInt(mask);

    uint32_t net_lower = (network_addr & mask_addr);
    uint32_t net_upper = (net_lower | (~mask_addr));

    if (ip_addr >= net_lower &&
        ip_addr <= net_upper)
        return true;
    return false;

And just to make sure that it works here is some test code.


void test(const std::string ip, const std::string network, const std::string mask, bool expected) {
    if (IsIPInRange(ip, network, mask) != expected) {
        printf("Failed! %s %s %s %s\n", ip.c_str(), network.c_str(), mask.c_str(), expected ? "True" : "False");
    } else {
        printf("Success! %s %s %s %s\n", ip.c_str(), network.c_str(), mask.c_str(), expected ? "True" : "False");

int main(int argc, char **argv) {
    std::string ip(argv[1]);

    test("", "", "", true);
    test("", "", "", false);
    test("", "", "", false);

    test("", "", "", false);
    test("", "", "", true);
    test("", "", "", false);
    test("", "", "", false);

    test("", "", "", true);
    test("", "", "", false);

    return 0;


E-mail Kick it! DZone it! Permalink

MSSQL - Finding the database restore history

7. August 2012 21:12


Did you know you can view the database restore history from MSSQL server. The following SQL will provide a list of all database restores that have been performed on a server at some point.



	destination_database_name, server_name AS 'SourceServer', database_name AS 'SourceDB', physical_device_name, type, backup_start_date, restore_date
	FROM msdb.dbo.restorehistory AS rh
		INNER JOIN msdb.dbo.backupset AS bs ON bs.backup_set_id = rh.backup_set_id
		INNER JOIN msdb.dbo.backupmediafamily AS bmf ON bmf.media_set_id = bs.media_set_id
	ORDER BY restore_date DESC;



It will produce output that looks like this.



Stev	DC01	Stev	E:\MSSQL-Backup\Stev.bak	D	2011-09-17 08:43:58.000	2011-09-17 09:10:25.283


E-mail Kick it! DZone it! Permalink

Linux - Killing all processes for a specific user

13. July 2012 08:00


Here is a few methods for killing tasks for a specific user in linux which may be required during account deletion or because somebody has managed to be an idiot and locked himself out with a fork bomb or some such.


The simple method is to use the utility called 'slay' which for debian / unbuntu and most other distrobutions is avilable if its not install you can install it using 'apt-get install slay'


It is very simple to use. Just running the command slay <username> and it will kill all of that users processes.



The other method to use when slay is not avilable is a combination of ps and kill. You can use the following command



kill -TERM `ps h --User nobody -o pid`



Understanding the above can other advantages as well because you can switch out the --User for --Group and kill processes by group id as well as for a specific user.

E-mail Kick it! DZone it! Permalink

Cisco - Howto enable scp file transfer

7. July 2012 11:16


By default cisco routers will come with scp file transfers disabled. Though they are easy enough to enable so that you can download / upload configurations to them using ssh / scp. Which is useful for doing backup's of router config files. In particular a lot of routers automatically.


The first part of the problem requires that you enable enough privalages to be able to access scp from a particular user. Normally there is a configuration line that looks like 'aaa authentication login default local' or like 'aaa authentication login default group radius local' if you are using radius authentication. You will need to add the exec permission to the same group.


you can do this with the following command


aaa authorization exec default  local

Or if using radius

aaa authorization exec default group radius local



Then you need to also enable the scp service which you can do with the following



ip scp server enable



At this stage you should now be able to scp the config file. From linux I would use the following 'scp <routerip>:startup-config mybackupfile

E-mail Kick it! DZone it! Permalink