Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stick up your best shell scripts

  • 18-09-2003 8:40am
    #1
    Closed Accounts Posts: 62 ✭✭


    I've been messing about with shell scripts the last few days and I'm fairly ****e but hey...

    Anyway, why don't you stick up your best shell scripts that make monotonous tasks quick, dead handy scripts, whatever...

    If it isn't included in linux and you or a friend wrote it stick it up and share the wealth...


«134

Comments

  • Closed Accounts Posts: 157 ✭✭BenH


    This might be of some interest, especially if your a redhat fan:

    http://night-shade.org.uk/basic-configs/


  • Registered Users Posts: 2,518 ✭✭✭Hecate


    I wrote this to automatically update the ports tree on my freebsd box; it has a cron entry to run every saturday morning, gotta keep up to date with the latest and greatest:
    #!/bin/sh

    IFTEST=`ifconfig tun0 |grep UP`

    if [ -n "$IFTEST" ] ; then

    /usr/local/bin/cvsup -g -L 2 /root/ports-supfile

    else

    ppp -ddial nolimits
    sleep 40
    /usr/local/bin/cvsup -g -L 2 /root/ports-supfile
    killall ppp
    fi

    The box also stores a fair amount of divxs at any one time, so I have a script to search out any movies that are on the server and output them to a html page:
    #!/bin/sh

    echo "<html><h1>Movie feed .. yum</h1><br><p align=left><pre>" > /usr/local/www/data/film_listing.htm

    find / -name '*.avi' >> /usr/local/www/data/film_listing.htm
    find / -name '*.ogm' >> /usr/local/www/data/film_listing.htm

    echo "</p></pre></html>" >> /usr/local/www/data/film_listing.htm

    Thats it for now, apart from a few one liners that do boring but useful stuff like dial up with ppp and set the time on the server using ntpdate.

    I had some pretty handy ones for backing up users home directories and mysql/pgsql accounts to tapes using a combination of tar and rsync on the DIT netsoc server, but it's offline at the moment :(


  • Registered Users Posts: 2,076 ✭✭✭parasite


    i've been googling for ways to total the amount of hours of my ppp sessions per month, but ntop etc seem like overkill, and i've been trying to think of a script to write for it, what's the best way to go about it, grep ps for pp every so often or something better ?
    :confused:


  • Closed Accounts Posts: 484 ✭✭ssh


    Get your boys out of there, this is gonna be a big one... Adds services from your current runlevel under debian...

    [PHP]
    #!/bin/sh

    echo addrc - Enable services at startup for Debian systems
    function print_current_services
    {
    \ls /etc/rc$rl.d/ |
    while read line; do
    order=$(echo $line | cut -b 2,3)
    echo $order: $(echo $line | sed -e 's/S[0-9]*//g')
    done
    }

    rl=$(runlevel | cut -d " " -f 2)
    service=XX

    while [ $service == XX ]; do
    echo -n "Enter the name of the service [Hit return to see a list]: "
    read service

    tmpvar=XX$service

    if [ $tmpvar == XX ]; then
    service=XX
    echo
    \ls /etc/init.d/
    echo
    fi

    \ls /etc/init.d/ | egrep -q ^$service$
    if [ $? != 0 ] && [ $tmpvar != XX ]; then
    echo Service does not exist
    service=XX
    fi
    done

    echo -n Checking to see if $service is already configured to run...
    \ls /etc/rc$rl.d | sed -e 's/S[0-9]*//g' | grep -q ^$service$ > /dev/null
    if [ $? == 0 ]; then
    echo Failed - already there
    exit
    fi
    echo passed

    echo "Here is a list of services and the order in which they start: "
    echo
    print_current_services

    sorder=XX
    while [ $sorder == XX ]; do
    echo
    echo -n "Enter the order you want the service to start in (10-99: "
    read sorder
    echo $sorder | egrep -q ^[0-9][0-9]$
    if [ $? != 0 ]; then
    echo Enter a number between 10 and 99
    sorder=XX
    fi
    done

    echo -n Adding service to list...
    ln -s /etc/init.d/$service /etc/rc$rl.d/S$sorder$service

    echo Checking to see if the service is listed for shutdown...
    \ls /etc/rc1.d/ | sed -e 's/K[0-9]*//g' | egrep -q ^$service$
    if [ $? != 0 ]; then
    ln -s /etc/init.d/$service /etc/rc1.d/K$sorder$service
    fi
    echo done

    echo "Here's how things look now:"

    print_current_services
    [/PHP]


  • Registered Users Posts: 1,862 ✭✭✭flamegrill


    here's a vhost adding script I fired togeter, its nasty but it works. Basically it adds the proper folders etc for the user/site in question. It prints the httpd.conf entry to screen and you can do what you like with that.

    I also wrote another version that does some fancy log work with cronolog. I'll leave that for another day.

    I've got a nice bit of c that I'll paste in a bit to check /var/log/messages for time online :)


    [PHP]

    #!/usr/bin/php -q
    <?

    if (isset($argv[1]))
    {

    $domain = $argv[1];
    $user = $argv[2];
    if (isset($argv[3]))
    {
    $vhostip = $argv[3];
    }
    else
    {
    $vhostip = "217.114.170.92";
    }
    $vhost = "
    <VirtualHost $vhostip>
    ServerName www.$domain
    ServerAdmin $user@$domain
    DocumentRoot /home/$user/$domain/web
    ServerAlias $domain
    DirectoryIndex index.html index.htm index.php index.php4 index.php3 index.shtml index.cgi index.pl
    ScriptAlias /cgi-bin/ /home/$user/$domain/cgi-bin/
    AddHandler cgi-script .cgi
    AddHandler cgi-script .pl
    ErrorLog /home/$user/$domain/logs/error_log
    CustomLog /home/$user/$domain/logs/access_log combined
    AddType application/x-httpd-php .php .php4 .php3
    AddType text/html .shtml
    AddHandler server-parsed .shtml
    #ErrorDocument 400 /error/invalidSyntax.html
    #ErrorDocument 401 /error/authorizationRequired.html
    #ErrorDocument 403 /error/forbidden.html
    #ErrorDocument 404 /error/fileNotFound.html
    #ErrorDocument 405 /error/methodNotAllowed.html
    #ErrorDocument 500 /error/internalServerError.html
    #ErrorDocument 503 /error/overloaded.htm
    </VirtualHost>
    ";

    print $vhost;

    // nasty shell code follows to make directories to keep apache happy :)
    $shellcode = "mkdir -p /home/$user/$domain/web; mkdir -p /home/$user/$domain/cgi-bin/; mkdir -p /home/$user/$domain/logs/ ";
    #passthru($shellcode);
    }

    else
    {
    print "Useage is as follows:\n\n";
    print "vhostadd.php <domainname> <user> optionally give vhost-ip (without the <>)\n";

    }
    ?>


    [/php]


  • Advertisement
  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    [EDIT: Ah, because of the domain. Tool. Never mind.]

    You can lose the multiple mkdir's, it accepts multiple directories.
    mkdir -p /home/$user/$domain/web /home/$user/$domain/cgi-bin /home/$user/$domain/logs
    
    HTH,
    adam


  • Registered Users Posts: 1,862 ✭✭✭flamegrill


    Originally posted by dahamsta
    [EDIT: Ah, because of the domain. Tool. Never mind.]

    You can lose the multiple mkdir's, it accepts multiple directories.

    mkdir -p /home/$user/$domain/web /home/$user/$domain/cgi-bin /home/$user/$domain/logs
    
    HTH,
    adam

    Ah yes indeedy, a less evil shell argument :)

    Still looking for a powercable for the old box with the pppd timer thing. :)


  • Registered Users Posts: 1,862 ✭✭✭flamegrill


    This isn't 100% accurate, but it will be good up to 1-2 hours of your actual usage. Saved me heaps on UTV in the later months :)


    [PHP]

    /*
    program to parse /var/log/messages for pppd dialup information
    by Paul Kelly (paul@dahomelands.net) 2002
    */



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define DATELEN 16



    typedef struct ppp_stat_st {
    struct {
    unsigned long connects, disconnects;
    unsigned long connected_secs;
    unsigned long sentk, rcvdk;
    } total;

    struct {
    unsigned long hup, term;
    } reasons;

    struct {
    unsigned long connected_secs;
    unsigned long sentk, rcvdk;
    } best;
    } pstat;




    void pppd_log_stat(pstat *ps, FILE *fp) {
    char buf[BUFSIZ], *p, *q;
    unsigned long l;


    while (fgets(buf, sizeof buf, fp)) {
    buf[sizeof buf - 1] = 0;

    if (! (p = strstr(buf + DATELEN, "pppd"))) /* not a pppd logline */
    continue;

    if (strstr(buf, "LCP Echo")) /* debug mode on :[ */
    continue;

    if (strstr(p, "Connection terminated.")) {
    ps->total.disconnects ++;
    continue;
    }

    if (strstr(p, "SIGHUP")) {
    ps->reasons.hup++;
    continue;
    }

    if (strstr(p, "signal 15.")) {
    ps->reasons.term++;
    continue;
    }

    if (q = strstr(p, "Connect time")) {
    l = strtoul(q+13, 0, 10) * 60;
    ps->total.connected_secs += l;

    if (ps->best.connected_secs < l)
    ps->best.connected_secs = l;

    continue;
    }

    if (q = strstr(p, "Sent ")) {
    l = strtoul(q+5, NULL, 10) / 1024;
    ps->total.sentk += l;

    if (ps->best.sentk < l)
    ps->best.sentk = l;

    if (! (q = strstr(p, "ived")))
    continue; /* should never happen */

    l = strtoul(q+5, NULL, 10) / 1024;
    if (ps->best.rcvdk < l)
    ps->best.rcvdk = l;
    ps->total.rcvdk += l;
    continue;
    }
    }
    }



    void paul_kelly_fmt_print(FILE *fp, pstat *ps) {
    fprintf(fp,
    "PPP INTERFACE USAGE REPORT (complete)\n"
    "\n"
    "%13lu ppp sessions\n"
    "%13lu Hours online\n"
    "%13lu Kilobytes sent\n"
    "%13lu Kilobytes received\n"
    "\n"
    "Averages for this period:\n"
    "\n"
    "%13lu Hours online\n"
    "%13lu Kilobytes sent\n"
    "%13lu Kilobytes received\n"
    "\n"
    "Best records for a single session:\n"
    "\n"
    "%13lu Hours online\n"
    "%13lu Kilobytes sent\n"
    "%13lu Kilobytes received\n"
    "\n"
    "Connection termination reason counts:\n"
    "\n"
    "%13lu Modem hangup\n"
    "%13lu Received sigterm\n"
    "\n",

    ps->total.disconnects,
    ps->total.connected_secs / 60 / 60,
    ps->total.sentk,
    ps->total.rcvdk,

    ps->total.connected_secs / 60 / 60 / ps->total.disconnects,
    ps->total.sentk / ps->total.disconnects,
    ps->total.rcvdk / ps->total.disconnects,

    ps->best.connected_secs / 60 / 60,
    ps->best.sentk,
    ps->best.rcvdk,

    ps->reasons.hup,
    ps->reasons.term
    );
    }



    int main(int ac, char **av) {
    pstat ps;
    char *filename = "/var/log/messages";
    FILE *fp;

    if (ac > 1)
    filename = av[1];

    if (! (fp = fopen(filename, "r"))) {
    perror(filename);
    return EXIT_FAILURE;
    }

    memset(&ps, 0, sizeof ps);
    pppd_log_stat(&ps, fp);

    fclose(fp);

    paul_kelly_fmt_print(stdout, &ps);
    return EXIT_SUCCESS;
    }

    [/PHP]

    If it won't copy/paste compile from here, i'll email people the code. :-)

    Currently writing a few bits n bobs for handy administration :), will post them if/when they ever get completed.


  • Closed Accounts Posts: 62 ✭✭jerk


    nice one lads, the vhost one will be dead handy.


  • Closed Accounts Posts: 484 ✭✭ssh


    Analyses squid logs... needs python-dns too. A little inaccurate also because it should divide by 1024, not 1000...

    [php]
    #!/usr/bin/python

    import DNS
    import re
    import sys

    DNS.ParseResolvConf()
    requests = {}
    results = {}
    hitsperhost = {}

    host = ''
    size = 0
    type = ''
    url = ''

    biggest = 0
    biggesthost = ''
    biggesturl = ''

    displayedsummary = 0

    deflog = '/var/log/squid/access.log'

    try:
    lf = sys.argv[1]
    except:
    lf = deflog

    try:
    f = open(lf , "r")
    except IOError:
    print "Could not open log file"
    sys.exit(1)

    while 1 > 0:
    line = f.readline()

    if line == '':
    break

    splitline = line.split()

    host = splitline[0]
    size = int(splitline[9])
    type = splitline[10]
    url = splitline[6]

    if displayedsummary == 0:
    print 'First access in log at ',splitline[3],splitline[4]
    displayedsummary = 1
    try:
    requests[host] = requests[host] + size
    hitsperhost[host] = hitsperhost[host] + 1
    except KeyError:
    try:
    requests[host] = size
    hitsperhost[host] = 1
    except:
    print "You don't seem to have emulate_httpd_log enabled"
    sys.exit(1)

    if size > biggest:
    biggest = size
    biggesthost = host
    biggesturl = url

    try:
    results[type] = results[type] + size
    except KeyError:
    try:
    results[type] = size
    except:
    print "You don't seem to have emulate_httpd_log enabled"
    sys.exit(1)

    try:
    print 'Last access in log at ',splitline[3],splitline[4]
    except NameError:
    print 'Log file is empty'
    sys.exit(1)

    print 'Statistics by Host:'
    for i in requests.keys():
    try:
    hostname = DNS.revlookup(i)
    except:
    hostname= i
    print (requests / 1000),'KB by',hostname,'in',hitsperhost,'requests'

    hits = 0
    misses = 0
    rehit = re.compile('.*HIT.*')

    print '\nStatistics by request result:'
    for i in results.keys():
    if rehit.match(i):
    hits = hits + results
    else:
    misses = misses + results

    print (results / 1000),'KB marked as',i

    percentagehits = float(hits) / float(misses + hits) * 100

    print 'Total Hits = ',hits / 1000,'KB'
    print 'Total Misses = ',misses / 1000,'KB'
    print '% Hits = ',percentagehits,'%'

    print 'Biggest download was',biggesturl,'by',DNS.revlookup(biggesthost),'at',biggest / 1000,'KB'
    [/php]


  • Advertisement
  • Registered Users Posts: 1,862 ✭✭✭flamegrill


    I've decided to make this a sticky as it will always be handy for n00bs :)

    Paul


  • Registered Users Posts: 196 ✭✭charlieroot


    Originally posted by Hecate
    I wrote this to automatically update the ports tree on my freebsd box; it has a cron entry to run every saturday morning, gotta keep up to date with the latest and greatest:


    You might want to put a pkgdb -u or pkgdb -fu in there somewhere otherwise you'll end up with some nasty problems.

    Noel.


  • Registered Users Posts: 1,419 ✭✭✭nadir


    erm, im kinda unsure what i can or cant put here, I have loads of small simple scripts ive thrown together but erm, im not sure if u could consider them all bash scripts, like i have a nice little xmms-irssi script that i rewrote from the irssi page with some updates and stuff like that. This might be way off topic, here is a very simple easy to follow firewall script, im not sure how secure this is but it functions fairly well against port scanners, allows internal nat, ip forwarding, masquarding, filteres tcp and udp ports, (allows UT2003, Quake3, Army Ops Recon and others). Basically it will allow outbound connections but not inbound and still allow routing from an internel network, in otherwords u should be able to use all ur fav apps, like irc(DCC), p2p, games web .etc

    #simple firewall, by nadir - 2003
    echo "loading irc modules"
    modprobe ip_conntrack_irc ports=$IRC_PORTS || SYSLOG
    modprobe ip_nat_irc
    #set vars
    UNPRIVPORTS="30000:35000" # unprivileged port range

    # Remove any existing rules from all chains
    echo "flushing previous rules"
    iptables --flush
    iptables -t nat --flush
    iptables -t mangle --flush

    echo "Starting nadirs firewall"
    echo 1 > /proc/sys/net/ipv4/ip_forward
    iptables -A INPUT -d 10.0.0.0/8 -i ppp0 -j DROP
    iptables -A INPUT -m state --state NEW,INVALID -j DROP
    iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
    iptables -A INPUT -p tcp --syn -j DROP

    #udp and tcp ports to allow.

    iptables -I INPUT 1 -p tcp -m multiport --dport 113,20045,9201,8481,8888 -j ACCEPT

    iptables -I INPUT 1 -p udp -m multiport --dport 1716,1717,1718,8777,27900 -j ACCEPT

    #This enables masquarding
    iptables -A POSTROUTING -t nat -o eth1 -j MASQUERADE

    # activate IP-Forwarding
    echo 1 > /proc/sys/net/ipv4/ip_forward

    echo "firewall rules implemented"
    echo "
    Rules are
    "
    #list rules
    iptables -t nat -nL
    iptables --list


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    echo "Welcome to the ftp user addition utility"
    echo "You can cancel this process at any time by"
    echo "Holding down CRTL and tapping the c key"
    echo " "
    echo "Please enter the username you'd like to create and press [ENTER]"
    read luser

    echo "User " $luser " Ok y/n?"
    read val

    if [ "$val" == "n" ]; then
    echo "Not continuing "
    exit -1
    elif [ "$val" == "N" ]; then
    echo "Not continuing "
    exit -1
    elif [ "$val" == "y" ]; then
    mkdir /usr/data/$luser
    /usr/sbin/useradd -d /usr/data/$luser -s /sbin/nologin $luser
    passwd $luser
    passwd -x 2 $luser
    /usr/sbin/usermod -G $luser,writers $luser
    /bin/chown $luser /usr/data/$luser
    /bin/chgrp writers /usr/data/$luser
    /bin/chmod g+w /usr/data/$luser
    /bin/chmod 777 /usr/data/$luser
    elif [ "$val" == "Y" ]; then
    mkdir /usr/data/$luser
    /usr/sbin/useradd -d /usr/data/$luser -s /sbin/nologin $luser
    passwd $luser
    passwd -x 2 $luser
    /usr/sbin/usermod -G $luser,writers $luser
    /bin/chown $luser /usr/data/$luser
    /bin/chgrp writers /usr/data/$luser
    /bin/chmod g+w /usr/data/$luser
    /bin/chmod 777 /usr/data/$luser
    else
    echo "You have not confirmned nor denied this input, exiting now"
    exit 0
    fi


    for file in $( ls -t *.mp3 *.ogg | head -250 | tr ' ' '_');
    do
    #scp $file root@10.5.2.113:/storage/mp3
    scp -C "`echo $file | sed 's/_/ /g'`" root@10.5.4.0:/storage/mp3
    done



    /data/pgsql-site-1.5/bin/vacuumdb -p 6543 -z stockbyte-dev;
    /data/pgsql-site-1.5/bin/pg_dump -p 6543 stockbyte-dev > /data1/site-15-db-backup/$1;
    /bin/rm /data1/site-15-db-backup/$1.bz2;
    /usr/bin/bzip2 /data1/site-15-db-backup/$1;


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Not exactly a shell script per se, but I run it from various shells :) (For some reasn I had to stop using watch under tcsh, which I used to inform me when some of "the lads" logged in; so I wrote this to do it)


    #!/usr/bin/perl -w
    
    use strict;
    
    my (%users, @in);
    
    while (<DATA>) {
      chomp;
      $users{$_} = 0;
    }
    
    for (;;) {
      @in = split/ /,`users`;
      foreach my $n (keys %users) { 
        if ( !$users{$n} && map {/^$n$/} @in ) {
          $users{$n} = 1;
          print scalar localtime, " $n has logged in \n";
        }
        if ( $users{$n} && ! map {/^$n$/} @in ) {
          $users{$n} = 0;
          print scalar localtime, " $n has logged out \n";
        }
      }
      sleep 2;
    }
    
    __DATA__
    hostyle
    
    add
    other
    users
    here
    
    


  • Registered Users Posts: 2,707 ✭✭✭niallb


    Nice one hostyle,
    I ended up added this pid line.
    Following the instructions in the comment allow you to automate starting and stopping the script.
    Multiple logins would make it break, but it's only an extra line or two to get around that.


    #!/usr/bin/perl -w
    # All bar one line by hostyle - boards.ie
    # the ~/.watcher.pid file is to allow you to kill the process in .bash_logout
    # Add similar lines to your rc to automatically run and close it.
    # Make a copy in your own ~/bin directory if you want to change userlist. :-)
    #
    # ## Add these to ~/.bash_profile ##
    # PATH=$PATH:~/bin
    # /usr/local/bin/watcher &

    # ## Add these to ~/.bash_logout ##
    # kill -HUP `cat ~/.watcher.pid`
    # cp -f /dev/null ~/.watcher.pid

    `echo $$ >~/.watcher.pid`;

    use strict; ...


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Originally posted by niallb
    Multiple logins would make it break, but it's only an extra line or two to get around that.

    "watch" does exactly the same - multiple logins = multiple reports. Removing duplicates from @in should fix it I think. Some methods here.

    I prefer shorter methods (like this) though:
    # @in = split/ /,`users`;
    @in = sort keys %{ {map {$_, 1} split/ /,`users`} };
    

    <edit>never mind me. this code is unnecessary as what niallb said below is true. i need stronger drugs </edit>


  • Registered Users Posts: 2,707 ✭✭✭niallb


    Sorry.
    To clarify, I meant multiple logins would break my automation which only remembers a single pid for watcher. :-)

    The script works intuitively - it reports first login and last logout as far as I can see, just what you need.

    NiallB


  • Registered Users Posts: 1,419 ✭✭✭nadir


    #!/bin/bash
    
    # this scripts was pwned by nadir 11/05/2004 - [email]nadir@zion.nuigalway.ie[/email] : for the fluxbox wm.
    #
    # It requires an entry like this in your fluxbox menu, 
    # [submenu] (Proc) {PID--Proc-State--Parent-Nice-Priority-%mem}
    # [include] (~/.fluxbox/proc)
    # [end]
    #
    # ok this is a simple script to gather relevant processes from the /proc stat files - and allow you to kill a process. Id like to do this in c, and have like a full, system monitor, maybe if there are likeminded prople in the fluxbox community who think this is a good idea, please let me know, Id like to contribute.
    # its a bit spammy and not terribly efficient even though i tried to keep it as small as possible, but i do find it usefull, I hope someone else out there find some use for it too :).
    #also i havn't really bugtested this much, so dont blame me if your computer fubars
    
    #kotoba
    
    
    #please set the refresh rate here in seconds, note it will slow stuff down if you set this too low.
    
    refresh=50
    
    x=0
    while [ $x -lt 1 ]
    do
    
    sleep $refresh
    
    proclist=$(ls /proc |grep [/0-9])
    totalmem=$(cat /proc/meminfo |cut -d ":" -f 2 |sed -e 's/^[ \t]*//' -e 's/kB/ /g' -e q1)
    procs=($proclist)
    
    noprocs=${#procs[@]}
    num_elms=$(echo "$noprocs - 1" |bc) 
    lastchild="2"
    
    for ((a=1; a <= $num_elms ; a++))
    do
    
    if [ -d /proc/${procs[a]} ];
    then procstat=$(echo /proc/${procs[a]}/stat) && procstatus=$(echo /proc/${procs[a]}/status);
    else procstat="/proc/self/stat" && procstatus="/proc/self/stat";
    fi
    
    memstatus[a]=$(cat $procstatus |grep VmRSS |cut -d ":" -f 2 |sed -e 's/^[ \t]*//' -e 's/kB/ /g')  
    
    if [ ${memstatus[a]} ];
    then procmem[a]=${memstatus[a]};
    else procmem[a]="1";
    fi
    
    
    
    rawstats[a]="$(cat $procstat | awk '{ print $1, $2, $3, $4, $18, $21 }' |sed -e 's/(/ /g' -e 's/)/ /g')"
    
    child=$(echo ${rawstats[a]} |cut -d " " -f 4)
    name=$(echo ${rawstats[a]} | awk '{ print $2 }')
    mempercentage[a]=$(echo "scale = 10; ${procmem[a]} / $totalmem *100" |bc |cut -b 1-4)
    
    if [ $child -ne 1 ] || [ $name != "cat" ];
    then processinfo[a]="[exec] (${rawstats[a]} ${mempercentage[a]}) {killall -9 $name} \\n";
    fi
    
    done
    echo -e \\n ${processinfo[@]} > ~/.fluxbox/proctemp 
    cat ~/.fluxbox/proctemp |column -t > ~/.fluxbox/proc
    done
    

    screenie


  • Registered Users Posts: 7 pixelbeat


    http://www.pixelbeat.org/scripts/
    I guess it's also worth mentioning these 1 line shell scripts :-)
    http://www.pixelbeat.org/cmdline.html


  • Advertisement
  • Closed Accounts Posts: 153 ✭✭Dustpuppy


    Have a look to this.
    A simple webserver based on shell script.


    #!/bin/sh -e

    # httpd.sh - small webserver
    # Karsten Kruse 2004 www.tecneeq.de, idea from httpd.ksh www.shelldorado.com
    #
    # Start via inetd: 80 stream tcp nowait nobody /usr/local/sbin/httpd.sh

    root=${HTTP_DOC_ROOT:-/home/httpd/htdocs/}

    gettype(){
    case "$1" in
    *.xhtml|*.html|*.htm|*.XHTML|*.HTML|*.HTM) type="text/html" ;;
    esac
    echo ${type:-$(file -i "$1" | awk '{print $2}' | sed 's/;//')}
    }

    while read line ; do
    [ "$line" = "$(printf \r\n)" ] && break # end of request header
    [ -z $requestline ] && requestline="$line" && break
    done

    set -- $requestline
    doc="$root/$2"
    [ -d "$doc" ] && doc="$doc/index.html"

    if [ "$1" != GET ] ; then
    printf "HTTP/1.0 501 Not implemented\r\n\r\n501 Not implemented\r\n"
    elif expr "$doc" : '.*/\.\./.*' >/dev/null ; then
    printf "HTTP/1.0 400 Bad Request\r\n\r\n400 Bad Request\r\n"
    elif [ ! -f "$doc" ] ; then
    printf "HTTP/1.0 404 Not Found\r\n\r\n404 Not Found\r\n"
    elif [ ! -r "$doc" ] ; then
    printf "HTTP/1.0 403 Forbidden\r\n\r\n403 Forbidden\r\n"
    elif [ -r "$doc" -a -f "$doc" ] ; then
    printf "HTTP/1.0 200 OK\r\nContent-type: $(gettype "$doc")\r\n"
    printf "Content-Length: $(wc -c < "$doc")\r\n\r\n"
    exec cat "$doc"
    else
    printf "HTTP/1.0 500 Server Error\r\n\r\n500 Server Error\r\n"
    fi

    # eof


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    Here's a couple of my one-liners to make some things easier:
    #!/bin/sh
    
    freshclam
    clamscan -r --log=/home/<usr>/clamlog /
    

    Just put in your own user name where <usr> is. It updates clamscan, runs a full scan of your HD (takes a while) and then outputs a log to your home folder. Its best run as root.
    #!/bin/sh
    
    /sbin/hdparm -d1 /dev/cdrom
    /sbin/hdparm -d1 -u1 -s180 -X66 -A1 -m16 /dev/hda
    

    This is a startup script linked into my run level ones. If you don't know your hd or hdparm, avoid this one. This will increase your hd and cd performance (dma is enabled on both) and also put my hd into full standby if I don't use it for 15 minutes.
    mount -t vfat /dev/sda1 /mnt/mem
    

    Mounts a flash drive, memory card or whatever else in /mnt/mem, as memory cards sometimes mount as several folders, which can be messy. This keeps them all in one place (need to mkdir /mnt/mem).


  • Registered Users Posts: 53 ✭✭martinoc


    :)


  • Registered Users Posts: 1,865 ✭✭✭Syth


    I use this little script when I'm root and modifying system files:
    #! /bin/bash
    
    CURRTIME=$(date '+%Y-%m-%d-%H:%M')
    
    FILE=$1
    
    DIRECTORY=$(pwd)
    
    CHANGEDFILES_PATH="${HOME}/changedfiles"
    
    cp $FILE ${FILE}_${CURRTIME}
    echo >> ${CHANGEDFILES_PATH}
    echo ${CURRTIME} >> ${CHANGEDFILES_PATH}
    echo "${DIRECTORY}/${FILE}" >> ${CHANGEDFILES_PATH}
    
    echo "Please enter a description:"
    cat >> ${CHANGEDFILES_PATH}
    

    Call it by passing a file. You must be in the directory of that file. It'll copy that file appending the current time and date to the end. It'll ask for a description which it stores in ~/changedfiles along with the file and time.

    The advantage of storing the filenames in ~/changedfiles is that it makes it easy to go back over it to see what you had to change to get XYZ to work.


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Syth wrote:
    I use this little script when I'm root and modifying system files:
    #! /bin/bash
    
    CURRTIME=$(date '+%Y-%m-%d-%H:%M')
    
    FILE=$1
    
    DIRECTORY=$(pwd)
    
    CHANGEDFILES_PATH="${HOME}/changedfiles"
    
    cp $FILE ${FILE}_${CURRTIME}
    echo >> ${CHANGEDFILES_PATH}
    echo ${CURRTIME} >> ${CHANGEDFILES_PATH}
    echo "${DIRECTORY}/${FILE}" >> ${CHANGEDFILES_PATH}
    
    echo "Please enter a description:"
    cat >> ${CHANGEDFILES_PATH}
    

    Call it by passing a file. You must be in the directory of that file. It'll copy that file appending the current time and date to the end. It'll ask for a description which it stores in ~/changedfiles along with the file and time.

    The advantage of storing the filenames in ~/changedfiles is that it makes it easy to go back over it to see what you had to change to get XYZ to work.

    Not to needlessly disparage you, but is that not what comments are usually designed for? Also you now need to manually call your script before or after you edit any file (you could at least invoke your chosen editor at the end) ? It seems a little pointless IMO (and that could be "just me"), but thanks for sharing all the same.


  • Closed Accounts Posts: 191 ✭✭vinks


    i dont think anyone has really shown newbies how to do for loops in a shell script so here is a little script that is handy for connecting to machines sequentially and running a command
    #!/bin/sh
    LIST=`seq 104 128`
    CMD="ps aux | grep cfservd"
    
    for i in $LIST;
    do
            echo node$i
            ssh node$i $CMD
    done
    


  • Registered Users Posts: 1,419 ✭✭✭nadir


    vinks wrote:
    i dont think anyone has really shown newbies how to do for loops in a shell script so here is a little script that is handy for connecting to machines sequentially and running a command
    #!/bin/sh
    LIST=`seq 104 128`
    CMD="ps aux | grep cfservd"
    
    for i in $LIST;
    do
            echo node$i
            ssh node$i $CMD
    done
    

    both hostyle and myself have use of for loops, but not the 'on the fly' type usage you are using.
    for ((a=1; a <= $num_elms ; a++))
    
    is what I like for shell scripts, just cause of its c like syntax , and also
    for (;;) {
      @in = split/ /,`users`;
    
    thats pretty funky,
    but yeah whatever like, it's all good


  • Closed Accounts Posts: 191 ✭✭vinks


    well, the pro of doing it my way, is you can walk an array and operate on that array, its particularly useful for working with things that arent numbers, you might not always have nice static names/ip's to play with. i guess the con is that its filthy scripting. its just handy for quickly doing things.


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    I've been teaching myself the basics of bash scripting, and here is my first crack at a half-decent script. Its for using clamav to scan your machine. Lots of commenting, so you can follow what's going on.

    I realise this is but one way of many to perform this task, but this one is to my ends :)
    #!/bin/bash
    
    #Ye olde clamscan script
    
    #First, lets set the variables...
    
    LOG=~/Log.txt			#This is the output log file from the scan
    QUAR=~/Quarantine		#This is the folder where we put any nasty crap we find. Kekeke
    ROOT_UID=0			#This is the root UID, for determination whether or not you're root..
    NOROOT=67			#What happens to non-root users..
    FOLDER=.			#Folder you want to scan from, default is the folder you're in and recursively from there.
    
    #First, we check if you're root...
    
    if [ "$UID" -ne "$ROOT_UID" ]
    then
    	echo "You're not root user"
    	exit $NOROOT
    else
    	echo "Please remember: This script will scan recursively from your current folder, not from /"
    fi
    
    sleep 5
    
    #Next, we either create the quarantine file, or remove any nastiness from it, from the last scan...
    
    : >> $QUAR
    
    #Thirdly, we update the virus database...
    
    echo
    
    freshclam
    
    sleep 5
    
    #And finally, ye olde virus scan...
    
    clamscan --log=$LOG --move=$QUAR -r -v -i $FOLDER 
    
    echo
    
    #Lastly, a reminder of the log and quarintine files
    
    echo "A log of this scan has been saved to $LOG and any quarantined files to $QUAR"
    

    Flame it, please


  • Advertisement
  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    Sorry to butt in... but can someone please explain how to implement these scripts?

    (sorry for the n00b reply)


Advertisement