You are not logged in.

#976 2010-04-07 16:37:03

alterecco
Member
Registered: 2009-07-13
Posts: 152

Re: Post your handy self made command line utilities

alexanderte wrote:

Please give me some feedback if you notice any security issues with this script. Hashing would make it more secure, but I'm not good at that stuff.

I would recommend looking at pwsafe

Offline

#977 2010-04-07 17:36:01

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

Daenyth wrote:

Make a wrapper script around it that invokes gpg

Or an alias.

alias pw='gpg -d $HOME/file.asc | grep'

To edit the file:

vi $HOME/file.asc

Offline

#978 2010-04-07 18:54:01

ataraxia
Member
From: Pittsburgh
Registered: 2007-05-06
Posts: 1,553

Re: Post your handy self made command line utilities

Why not just use pwsafe?

Offline

#979 2010-04-07 19:35:54

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Post your handy self made command line utilities

ataraxia wrote:

Why not just use pwsafe?

I'm currently using a gpg'd file as I have a load of passwords in keepassx format but don't really want to use keepassx any more - (for no real rational reason probably, but let's go with that for now) and I really couldn't be bothered to type them all in - esp. as I have to type in my passphrase for every single one. If anyone knows of a way to import numerous items from keepassx to pwsafe, then I'd love to hear about it.

Otherwise, to keep this on topic, I like that xclip script and may well have a look at combining it with gpg, if I get a few minutes.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#980 2010-04-08 17:15:33

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Post your handy self made command line utilities

I wanted an excuse to use paste, and I find that I sometimes forget what FS I'm using on my partitions, so I whipped up an "improved" df command:

#!/bin/bash
paste <(echo "Type";/bin/mount | sed -n '/^\/dev/s/.*type \([a-z0-9]*\) .*/\1/p') <(/bin/df -h | grep -E "^\/dev|^Filesystem")

Example output:

Type    Filesystem            Size  Used Avail Use% Mounted on
btrfs    /dev/sdb3             149G  2.4G  147G   2% /
ext2     /dev/sda1              92M   12M   75M  14% /boot
ext4     /dev/sdb2              20G  2.5G   17G  14% /home
ext4     /dev/sdc1             367G   56G  293G  16% /mnt/Destruction
btrfs    /dev/sdb4             200G   21G  180G  11% /mnt/Entropy
btrfs    /dev/sdd1             932G  502G  430G  54% /mnt/Gluttony

edit: Fix alignment. Silly BBS.

Last edited by falconindy (2010-04-08 17:16:27)

Offline

#981 2010-04-08 17:20:35

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

Does 'df -Th' not do the same?

Offline

#982 2010-04-08 18:30:12

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Post your handy self made command line utilities

steve___ wrote:

Does 'df -Th' not do the same?

!@#$%^!@#$

Ah well.

Offline

#983 2010-04-08 19:50:00

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: Post your handy self made command line utilities

falconindy wrote:
steve___ wrote:

Does 'df -Th' not do the same?

!@#$%^!@#$

Ah well.

thanks to both of you. I also have been typing 'mount' and 'df -h' after each other way too many times, without knowing the -T option. So i can definitely understand falconindy's idea to DIY wink

btw what's this 'paste' thing?


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#984 2010-04-08 20:33:57

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Post your handy self made command line utilities

paste is the horizontal cousin of cat.

Offline

#985 2010-04-09 05:57:36

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

Nice one line script though, falconindy.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#986 2010-04-09 06:15:41

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

Talking of df.
I've missed an total free/used summary (THAT is for sure not in df... I hope).
So I made this hack, pdf:

#!/usr/bin/perl 
# pdf
use strict;
use Filesys::DiskFree;

my $df = Filesys::DiskFree->new;
my @disks = glob('/mnt/*');
push(@disks, '/');

my (@total, @free, @used);

printf("\033[31;1m%11s %11s %12s %9s\033[0m\n",
      'MOUNTPOINT', 'TOTAL', 'WASTED', 'FREE');
foreach my $drive(@disks) {
  $df->df();
  printf("%-13s | %6.2f GB | %6.2f GB | %6.2f GB\n",
        $drive, &btogb($df->total($drive)), &btogb($df->used($drive)),
        &btogb($df->avail($drive)));

  push(@total, $df->total($drive));
  push(@free,  $df->avail($drive));
  push(@used,  $df->used($drive));
}

printf '-' x 50;
my $c  = "\033[34;1m";
my $c0 = "\033[0m"; 
printf("$c\nSUMMARY$c0\n FREE: %-5.2f  GB\n USED: %.2f GB\nTOTAL: %.2f GB\n",
       &calculate(@free), &calculate(@used), &calculate(@total));

sub calculate {
  my @total = @_;
  my $sum;
  foreach my $total(@total) {
    $sum += $total;
  }
  return &btogb($sum);
}

sub btogb {
  my $foo = shift;
  return $foo/1024/1024/1024;
}

I also wrote a tailer for the Common Log Format (apache, lighttpd etc) that'll make it a lil bit easier to see what's going on using color codes. There was some guy in #archlinux that wanted this, but I forgot who, so I hope he see this. tongue

#!/usr/bin/perl 
use strict;
use File::Tail;

my $log   = shift;
my $line  = "";
my $tail  = File::Tail->new(name=>$log,
                            maxinterval=>3,
                            adjustafter=>3,
                            interval=>0,
                            tail=>100
                            );
while(defined($line=$tail->read)) {
  my $e = "(.+?)";
  $line =~ /^$e $e $e \[$e:$e $e\] "$e $e $e" $e $e/;

  my $ip      = $1;
  my $ref     = $2;
  my $name    = $3;
  my $date    = $4;
  my $time    = $5;
  my $gmt     = $6;
  my $request = $7;
  my $file    = $8;
  my $ptcl    = $9;
  my $code    = $10;
  my $size    = $11;

  $code = "\033[38;5;240m$code\033[0m" if $code == 404;
  $code = "\033[38;5;155m$code\033[0m" if $code == 200;
  $code = "\033[38;5;160m$code\033[0m" if $code == 501;
  $code = "\033[38;5;208m$code\033[0m" if $code == 301;
  $code = "\033[38;5;124m$code\033[0m" if $code == 403;
  $code = "\033[38;5;113m$code\033[0m" if $code == 304;
  $file = "" if $file =~ /^\/$/;
  $file = "\033[38;5;160m$file\033[0m" if $file !~ /([A-Za-z0-9])+/;
  $size = "\033[38;5;160m$size\033[0m" if $size > 5;
  printf("%s %7s %s %s %60s\n",
  $code, $request, $size, $ip, $file);
}

Offline

#987 2010-04-09 13:38:22

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

dmz wrote:

I also wrote a tailer for the Common Log Format (apache, lighttpd etc) that'll make it a lil bit easier to see what's going on using color codes. There was some guy in #archlinux that wanted this, but I forgot who, so I hope he see this. tongue

pacman -S colortail

Offline

#988 2010-04-09 18:47:08

bluewind
Administrator
From: Austria
Registered: 2008-07-13
Posts: 172
Website

Re: Post your handy self made command line utilities

brisbin33 wrote:
dmz wrote:

I also wrote a tailer for the Common Log Format (apache, lighttpd etc) that'll make it a lil bit easier to see what's going on using color codes. There was some guy in #archlinux that wanted this, but I forgot who, so I hope he see this. tongue

pacman -S colortail

and I suggest tailf $log | ccze wink

Offline

#989 2010-04-12 15:04:12

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Post your handy self made command line utilities

When ghpoto2 imports photos from my camera they are named something along like "IMG_somenumber.JPG" and often I want to select more than one, but not all of them (usually a range) for some kind of action, so I've come up with this little tool:

#!/usr/bin/env perl
use strict;
use warnings;

my $re;
my $m;
my $cond;

if (@ARGV == 3) {
    ($re, $m, $cond) = @ARGV;
}
elsif (@ARGV == 2) {
    ($re, $cond) = @ARGV;
    $m = "";
}
else {
    print STDERR "pfilt pattern [modifier] condition\n";
    exit 1;
}

eval("if (/$re/$m){print if $cond}") for (<stdin>);

e.g if I have the files IMG_0013.JPG IMG_0017.JPG and IMG_0018.JPG and run pfilt 'IMG_(\d+).JPG' '$1>13'
the last two will be printed. I haven't looked into all the flags of grep, and I'm not that fond of awk, so it is possible that I'm reinventing the wheel...but if I learn stuff doing it it is worth it imo.

Last edited by tlvb (2010-04-12 15:39:25)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#990 2010-04-12 17:36:37

TaylanUB
Member
Registered: 2009-09-16
Posts: 150

Re: Post your handy self made command line utilities

Haters gonna hate...
I guess this is what you'd call hackish code.

#!/bin/sh
#
# mountall.sh: Mount '/dev/$1?*'.
#
# Automatically creates (and later removes) relevant dirs in /mnt.
#

err_usage ()
{
    echo "Usage:"
    echo "'$(basename "$0")' dev_name [args_to_mount...]"
    echo
    echo "'dev_name' must be like 'hda', 'sda', 'hdb', 'sdb', etc."
    exit 1
}

test $# -lt 1 && err_usage

dev="$1"; shift

clearup="${TMPDIR:-/tmp}"/mountall-clearup--$$
echo > "$clearup" 'rm "$0"'
chmod u+x "$clearup"

{
trap "exec '$clearup' > /dev/null 2>&1" EXIT;
while true; do sleep 30; done
} &

for part in $(ls /dev/"$dev"?* 2> /dev/null)
do

    dir=/mnt/"$(basename "$part")"

    exists=
    test -d "$dir" && exists=yeah || {
        echo >> "$clearup" "umount '$dir'; rmdir '$dir'"
        mkdir "$dir"
    }

    mount "$part" "$dir" "$@" 2> /dev/null && {
        echo "mounted: $part"
        echo >> "$clearup" "printf \"\$(cat /etc/mtab)\" | grep -F '$part $dir' && umount '$dir'"
        success=yeah
    } || {
        test "$exists" || rmdir "$dir"
    }

done

test "$success" || { echo "mounted nothing"; exit; }

(edit: Code has been entirely changed. I hadn't tested it, and it was broken to the point of requiring a rewrite.)

Last edited by TaylanUB (2010-04-14 21:17:03)


``Common sense is nothing more than a deposit of prejudices laid down by the mind before you reach eighteen.''
~ Albert Einstein

Offline

#991 2010-04-13 17:20:14

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Daenyth wrote:

I just wrote this git pre-commit hook. It's intended for those who use git to manage their PKGBUILD files. It forbids all commits that include a PKGBUILD file when the source and install files are not either in git, or also in the commit.

Install it by putting it into .git/hooks/pre-commit and making it mode +x.

Source here

Just updated this with a bugfix, should be a bit more robust now smile

Offline

#992 2010-04-14 13:40:04

TaylanUB
Member
Registered: 2009-09-16
Posts: 150

Re: Post your handy self made command line utilities

I really gotta test my scripts before publishing them. hmm
The one i posted above is borked really hard. (Will edit in a minute.)


``Common sense is nothing more than a deposit of prejudices laid down by the mind before you reach eighteen.''
~ Albert Einstein

Offline

#993 2010-04-18 15:32:45

Chimera
Member
From: Run Level 7
Registered: 2010-03-28
Posts: 43

Re: Post your handy self made command line utilities

Wrote this this morning. (although I've wanted to for a while)
It opens up all files for a project you're working on, identified by the directory's name.
So if you're in directory "foo" it will open up foo.py/foo.pl/foo.c/whatever, and all (local) files it imports/uses/#includes/whatever

#!/usr/bin/perl

#This script opens the files for a project
#It determines the main file from the name of the directory, then opens it and all local modules it relies on in an editor of your choice

#file extensions
#    if you add a new language, make sure to add it to the regex (line 30)
$perl_ext="pl|p";
$python_ext="py";
$c_ext="c";
$cpp_ext="cpp|cc";

#import syntax for different languages
$perl_imp="use (\w+)";
$python_imp="import (\w+)|from (\w+) import";
#local includes only. Also note that c++ and c use the same syntax
$c_imp="#include \"(.+)\"";

#map the syntaxes to languages
%import_syntax=($perl_ext=>$perl_imp, $python_ext=>$python_imp, $c_ext=>$c_imp, $cpp_ext=>$c_imp);

#set the editor
$editor = shift || "gedit";

#project name
`pwd`=~/.*\/(.*)/;
$project=$1;

#find the main file
`ls`=~/($project\.($perl_ext|$python_ext|$c_ext))/;
$project_language=$2;
$import=$import_syntax{$project_language};

#open the files
@files=($1);
Find_Dependencies($1);

system("$editor @files");

#recursively search through project files, identifying imported (local) modules
sub Find_Dependencies
{
    local *FILE;
    open(FILE, shift) or return;    #local modules only;
    while ($_=<FILE>)
    {
        if (/$import/)
        {
            push(@files, $+);
            Find_Dependencies($+);
        };
    };
}

Note that perl can't make local filehandles, hence "local *FILE". Also, I know most IDEs do this for you anyways, but this could open up te files in vim, emacs, gedit, geany, or anything else under the sun. Heck, it could "open" them with cat, too...

Last edited by Chimera (2010-04-18 15:33:42)

Offline

#994 2010-04-19 15:34:37

muunleit
Member
From: Germany
Registered: 2008-02-23
Posts: 234

Re: Post your handy self made command line utilities

get your global and local ip with this little lua script

#!/usr/bin/env lua

require 'socket.http'

function matchIP( ip_string )
    if ip_string then
        return '\27[1;32m' .. string.match(ip_string, '%d+%.%d+%.%d+%.%d+') 
    else 
        return '\27[1;31mnot connected'
    end
end

local ip         = {}
ip['local']     = matchIP(io.popen('ip -o -f inet addr show scope global'):read())
ip['global']    = matchIP(socket.http.request('http://www.whatsmyip.us/showipsimple.php'))

for key, val in pairs(ip) do
    io.write(':: \27[1;34m'..key..'    '..val..'\27[0m\n')
end

os.exit()

Last edited by muunleit (2010-04-19 16:17:38)


"The mind can make a heaven out of hell or a hell out of heaven" -- John Milton

Offline

#995 2010-04-19 16:25:01

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Post your handy self made command line utilities

What is my ip? has an automation address for you to use in scripts.  See here: http://www.whatismyip.com/automation/default.asp


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#996 2010-04-22 05:46:29

Sirupsen
Member
From: Denmark
Registered: 2009-11-14
Posts: 26
Website

Re: Post your handy self made command line utilities

I posted a handy screen utility a while ago, I changed it a bit since then, mostly that it now uses Imgur and doesn't save the screenshots locally.

function uploadImage {
  curl -s -F "image=@$1" -F "key=486690f872c678126a2c09a9e196ce1b" http://imgur.com/api/upload.xml | grep -E -o "<original_image>(.)*</original_image>" | grep -E -o "http://i.imgur.com/[^<]*"
}

scrot -s "shot.png" 
uploadImage "shot.png" | xclip -selection c && rm "shot.png"
#notify-send "Done"

Pretty fast too. smile

(You should request to get your own key instead of using mine though, but it's no problem if you use it for testing, or minor use.)

Also, I made a script which makes sure only one terminal is running at a time. Very handy when you've bound f.e. Sakura to Alt-T, and use it every time you want a terminal - then you'd usually end up with a good load.

#! /bin/bash
WINTITLE="sakura" # Name of the window (or part of it)
PROGRAMNAME="sakura" # Name of the program, so it can be opened if there's no window currently

# Lists all windows, if there's one containing $WINTITLE it'll return 1, and bring the current instance of the program to the front.
if [ `wmctrl -l | grep -c "$WINTITLE"` != 0 ]
  then
    wmctrl -a "$WINTITLE"
# Else, it'll launch a new instance
else
  $PROGRAMNAME &
fi

# We're good!
exit 0

And then I recently threw this in my bin as c2c, used it a lot..

cat $1 | xclip -selection c

Last edited by Sirupsen (2010-04-22 05:49:41)

Offline

#997 2010-04-22 06:53:34

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

Sirupsen wrote:

And then I recently threw this in my bin as c2c, used it a lot..

cat $1 | xclip -selection c

Kill the cat. You might be interested in this as well; http://bbs.archlinux.org/viewtopic.php?id=95345

Offline

#998 2010-04-22 13:46:30

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Post your handy self made command line utilities

I use abook for my contacts. One of the advantage is the easy to deal with storage (plain text file). This little script will look-up a contact (or contacts) based on the search term and field (if specified). 99% of the time I use the nick field, so that's my default. I've set-up two formats - plain and latex - newlfm. I use the latter to insert into a newlfm letter template (I call this script from a separate one that inserts the address as returned here and the body as saved in a separate file into the template, then calls pdflatex to generate the letter).

It should be fairly simple to add new output formats as needed and these could be put into separate files for reference from the (g)awk routine. The script has not been checked for portability.

#!/bin/bash
#
# getcontact
#
# Returns a contact's address from
# the addressbook.

# Setup
ADBOOK=$XDG_DATA_HOME/abook/addressbook
SFIELD=nick

while getopts "f:h:t" opt; do
    case $opt in
        f  ) SFIELD="$OPTARG" ;;
        h  ) echo "Need to add help"
             exit 0 ;;
        t  ) tex="-v astex=1"  ;;
        \? ) echo "Need to add help"
             exit 1
    esac
done
shift $(($OPTIND -1))

LOOKUP=${1?"No search term specified"}
echo $LOOKUP

gawk $tex '

    # prints address without any formatting
    function printPlain(items) {
        if ("name" in items) print items["name"]
        if ("job_title" in items) print items["job_title"]
        if ("department" in items) print items["department"]
        if ("organisation" in items) print items["organisation"]
        gsub(",", "\n", items["address_lines"])
        if ("address_lines" in items) print items["address_lines"]
        if ("city" in items) print items["city"]
        if ("state" in items) print items["state"]
        if ("zip" in items) print items["zip"]
        if ("country" in items) print items["country"]
    }

    # prints address in tex format
    function printAsTex(items) {
        for (item in items) gsub(/&/, "\\\\&", items[item])
        if (items["name"])
            address  = "\\name{" items["name"] "}%\n\\addr{"
        if (items["job_title"])
            address = address items["job_title"] "\\\\%\n"
        if (items["department"])
            address = address items["department"] "\\\\%\n"
        if (items["organisation"])
            address = address items["organisation"] "\\\\%\n"
        if (items["address_lines"]) {
            gsub(", *", "\\\\%", items["address_lines"]) 
            address = address items["address_lines"] "\\\\%\n"
        }
        if (items["city"])
            address = address items["city"] "\\\\%\n"
        if (items["zip"])
            address = address items["zip"] "\\\\%\n"
        if (items["country"])
            address = address items["country"] "\\\\%\n"
        print "\\setadrto{\n" address "}}%"
    }

    BEGIN { FS = "="; inRec = 0 }

    /\[[0-9]+\]/   {    inRec = 1   }

    /^[:space:]*$/ {    inRec = 0
                        if (addr["'"$SFIELD"'"] == "'"$LOOKUP"'") {
                            if (astex == 1)
                                printAsTex(addr)
                            else
                                printPlain(addr)
                        }
                        else
                        {
                            for (item in addr) delete addr[item]
                        }
                    }
    /.*=.*/         {   if (inRec = 1) addr[$1] = $2  }' $ADBOOK

"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#999 2010-04-22 19:56:51

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

rson451 wrote:

What is my ip? has an automation address for you to use in scripts.  See here: http://www.whatismyip.com/automation/default.asp

thanks, thats usefull.

threw this mail notifier together. will email  you if your ip has changed.

#!/bin/bash 
ip=$(curl http://www.whatismyip.com/automation/n09230945.asp)
iplog=/tmp/.ip.log

if cat $iplog | grep -q $ip ; then
echo ip is the same as last run
else
echo -e "$(date) : - IP Change. \n\n your new ip address is $ip" | mail -s "[IP change] $ip" Email@domain.com 

fi

echo $ip > $iplog

edit....1000th post in thie thread smile

Last edited by markp1989 (2010-04-22 20:02:39)


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#1000 2010-04-22 20:53:26

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

hrm, i have something quite similar...

#!/bin/bash
#
# pbrisbin 2009
#
# http://pbrisbin.com:8080/bin/checkip
#
###

logger() { echo "$(date +'[ %d %b %Y %H:%M ]') $*" | tee -a "$log"; }

update_dns() {
  echo $new > "$ipfile"
  wget -O - --http-user=$zuser --http-passwd=$zpass 'http://dynamic.zoneedit.com/auth/dynamic.html?zones=pbrisbin.com' &>/dev/null || exit 1
  logger "ip updated $old -> $new"
}

[ -f "$HOME/.credentials" ] && . "$HOME/.credentials" || exit 1

ip_url='http://tnx.nl/ip'
log="$HOME/checkip.log"
ipfile="$HOME/.myip"
touch "$ipfile"

old="$(cat "$ipfile")"
new="$(lynx -dump "$ip_url")"

[ "$old" != "$new" ] && update_dns || exit 0

doesn't email me, but it updates my dns with zonedit and logs the occurrence.

woot! 9th post in this thread!

/binarylol

Last edited by brisbin33 (2010-04-22 20:54:30)

Offline

Board footer

Powered by FluxBB