Categories
Freebsd

Creating command line options in a bash script

Ok, so for a while I have done this a bunch of different ways but I think the best way I found is with a simple case statement. In here you can set variables, execute commands, functions, nest statements etc… It works out really well. here is a snippet to show you what i mean.

while [ $# -gt 0 ]
do
        case $1
        in
        -v)
                VERBOSE=YES
                shift 1
     
        ;;

        -l)
                echo "$MODULES"
                exit 0
        ;;

        -os)
                if [ -z $2 ]
                then
                        echo "You must choose a VERSION"
                        exit 1
                else
                        for MOD in $MODULES
                        do
                                if [ "$MOD" = "$2" ]
                                then
                                        LISTED=YES
                                fi
                        done
                        if [ "$LISTED" = "YES" ]
                        then
                                VERSION=$2
                                shift 2
                        else
                                echo "Module not found. Use -l for listing"
                                exit 1
                        fi
                fi
        ;;

        -h)
                HELPMENU
                exit 1
        ;;

        –help)
                HELPMENU
                exit 1
        ;;

        *)
                HELPMENU
                exit 1
        ;;
        esac
done

 

Categories
Freebsd

Passwordless ssh login with keys

Create a key with no passphrase on the source machine

[install@source ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/install/.ssh/id_rsa):
Created directory ‘/home/install/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/install/.ssh/id_rsa.
Your public key has been saved in /home/install/.ssh/id_rsa.pub.
The key fingerprint is:
66:c1:3f:95:c6:a9:d6:99:49:bf:a0:57:4f:a3:78:49 install@localhost.localdomain

Copy it to the destination machine…

[install@source ~]$ scp id_rsa.pub 10.50.2.50:~

On the destination machine create the .ssh directory and copy the id_rsa.pub to authorized_keys. If you have multiple keys you can just append it, something like this “cat id_rsa.pub >> authorized_keys”

[install@destination ~]$ mkdir .ssh
[install@destination ~]$ chmod 700 .ssh
[install@destination ~]$ cp id_rsa.pub .ssh/authorized_keys

Now you should be able to login…

[install@source .ssh]$ ssh 10.50.2.50
Last login: Fri Sep  5 14:18:01 2008 from 10.50.2.51
[install@destination ~]$

Categories
Openbsd

Creating a memory leak on Linux/Unix

I recently had to create a memory leak to do some testing, here is a snippet of code that I used to create a leak. You can adjust malloc size to allocate more memory, or adjust the usleep to adjust how aggressive it is. the code below on a machine with 500M of memory would chew up about 1% memory a second. Enjoy!

#include <stdlib.h>
#include <unistd.h>

int main(void) {
    /* An infinite loop. */
    while (1) {
       /* Try to allocate some memory. */
       malloc(8386080);
       /* sleep for a bit so its not so aggressive */
       usleep(10000);
    }
    return EXIT_SUCCESS;
}