You are not logged in.

#1726 2012-03-15 22:47:31

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Post your handy self made command line utilities

This is simplified version of a script I use to show differences between directory trees.

#!/bin/bash

# changes to go from $1 -> $2

src="$( find "$1" | sed "s!^$1!!" | sort )"
tgt="$( find "$2" | sed "s!^$2!!" | sort )"

lose="$( comm -23 <( echo "$src" ) <( echo "$tgt" ) | sed 's,^,- ,' )"
gain="$( comm -13 <( echo "$src" ) <( echo "$tgt" ) | sed 's,^,+ ,' )"

diff=''
while read file
do
   [[ -z "$file" ]] && continue
   diff -q "$1/$file" "$2/$file" &>/dev/null ||
   {
      [[ -z "$diff" ]] \
         && diff="x $file" \
         || diff="$( echo -e "$diff\nx $file" )"
   }
done < <( comm -12 <( echo "$src" ) <( echo "$tgt" ) )

[[ -n "$lose" ]] && out="$lose"

[[ -n "$gain" ]] &&
{
   [[ -z "$out" ]] \
      && out="$gain" \
      || out="$( echo -e "$out\n$gain" )"
}

[[ -n "$diff" ]] &&
{
   [[ -z "$out" ]] \
      && out="$diff" \
      || out="$( echo -e "$out\n$diff" )"
}

echo "$out" | sort -k2

Edit: bug

Last edited by fsckd (2012-03-18 23:43:06)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#1727 2012-03-15 23:55:37

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

fsckd wrote:

[...]

I wanted to compare your script with the quick hack I've been using to compare my backups, however when attempting to use it I get quite a few errors:

My mistake, ran your script using sh instead of bash. (Thanks Karol.) Only problem now is comparing large directories takes far too long (due to 'diff -q'). It had run for 8 minutes so far before I stopped it.

At any rate, here's my very basic directory (top level) comparison function. I'm not sure how it relates to yours, but it's short and extremely fast.

dirdiff() 
{ 
  diff --suppress-common-lines -y <(cd "$1"; ls -aX1 | sort) <(cd "$2"; ls -aX1 | sort)
}

If I wanted to compare deeper down the directories I've got one using 'find' (-maxdepth) which requires some stream editing to remove the first few directories. (e.g. '/home/user/'<stuff> compared '/media/backups/home/'<stuff>). All ears if there's a better way or a method to strip those first few directories.

Last edited by Earnestly (2012-03-16 00:59:17)

Offline

#1728 2012-03-16 00:13:20

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

@Kaustic
You should be running fsckd's script using bash ...

Offline

#1729 2012-03-16 05:37:09

swanson
Member
From: Sweden
Registered: 2011-02-05
Posts: 759

Re: Post your handy self made command line utilities

Here's a script I made for burning ogg files to audio cd from a Thunar Custom Action;

#!/bin/bash
# Script for converting, normalizing and burning ogg files, to audio cd.
# It is made as a Thunar custom action (uca) and is called from Thunar with 'scritpname %d'
# With modifications it can easily be used for other purposes.
# Requirements; vorbis-tools, mp3cd 1.027, thunar. mp3cd is called from another script, here called
# burnfromm3u

# Go thru the ogg-files and convert them

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# set me
FILES="$1"/*.ogg
for f in $FILES
do
  
NAME=$(basename "$f" .ogg)

ogg123 -d wav -f "$NAME".wav "$f"
done

# Create M3U file list for mp3cd which also can be used for players

touch "${PWD##*/}WAV.m3u"
find -type f \( -iname '*.wav' -o -iname '*.WAV' \) | sort >> "${PWD##*/}WAV.m3u"

# Call mp3cd to do its magic

burnfromm3u "${PWD##*/}WAV.m3u"

exit 0

burnfromm3u;

#!/bin/bash
# this script can burn ogg, mp3 and flac files to audio cd using mp3cd from the AUR
# Usage 'scriptname filename.m3u' or in Thunar 'scriptname %f'
terminal --geometry=100x24 --hold -e "perl /usr/bin/site_perl/mp3cd -v -c \"--speed 8 --buffers 128\" -T -d /dev/sr0 '$@' "
exit 0

It will leave the created wav-files in the starting directory, which might be useful or not. mp3cd is supposed to do this directly but I never got it to work with ogg files without the above script. mp3cd 1.027 does however work for mp3 and flac as is.

mp3cd; https://aur.archlinux.org/packages.php?ID=35842 (check the PKGBUILD in the post for version 1.027)

Last edited by swanson (2012-03-16 05:38:45)

Offline

#1730 2012-03-16 12:17:18

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Post your handy self made command line utilities

@Kaustic: Are you referring to something like -mindepth ? You can use -mindepth 1 -maxdepth 1 to restrict results to things exactly one directory deep.

If you mean so that if you pass d1/ and a/d2/ and want the paths d1/thing/file and a/d2/thing/file to be equal, what you have already works, since each find (or ls) should be relative to the path you cd too.

Do you really need the -X switch? You're passing the output to sort anyway, so I don't see how -X is doing any good.

Offline

#1731 2012-03-16 13:12:26

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

jac wrote:

@Kaustic: Are you referring to something like -mindepth ? You can use -mindepth 1 -maxdepth 1 to restrict results to things exactly one directory deep.
If you mean so that if you pass d1/ and a/d2/ and want the paths d1/thing/file and a/d2/thing/file to be equal, what you have already works, since each find (or ls) should be relative to the path you cd too.

Ah see, I'm being a bit deceptive. The ls variant works perfectly but it's restricted to one directory deep (?). I've made another which uses find with -maxdepth. It all works quite well except for one issue.

My home directory is: /home/earnest/ and my backup directory is /media/backup/home/. When using find (even if you cd first) it will include '/home/earnest/<thing_to_compare>' and '/media/backup/home/<thing_to_compare>'. The problem is when piping the contents to diff it will (correctly) see '/home/earnest...' and '/media/backup/home...' as different. A very bad workaround for this was to simply use sed to remove the offending directories (e.g. sed 's/\home\/earnest\///').

So my question was really if there was a better way to accomplish this.
(I've tried a for loop using basename, which is... interesting, lol. It works well enough but using a higher -maxdepth than 1 results in comedy.)

jac wrote:

Do you really need the -X switch? You're passing the output to sort anyway, so I don't see how -X is doing any good.

Good call, I'm not sure why it was added but you're quite right, I don't need it. Thanks smile

Offline

#1732 2012-03-16 13:33:29

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

Re: Post your handy self made command line utilities

@Kaustic This is from commandlinefu website, and it should do what you want as it cds to dir first, then does find on . so the path always starts ./foo. It's a bit of a mix between your two - I assume anyway.

diff --suppress-common-lines -y <(cd path-to-dir1; find .|sort) <(cd path-to-dir2; find .|sort)

I tries to run on current dir if the cd fails, so you could change the ; to && avoid that if you type the path wrong.


"...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

#1733 2012-03-16 14:23:56

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

skanky wrote:

@Kaustic This is from commandlinefu website, and it should do what you want as it cds to dir first, then does find on . so the path always starts ./foo. It's a bit of a mix between your two - I assume anyway.

diff --suppress-common-lines -y <(cd path-to-dir1; find .|sort) <(cd path-to-dir2; find .|sort)

I tries to run on current dir if the cd fails, so you could change the ; to && avoid that if you type the path wrong.

Perfect, all working now.
I was making the dumb mistake of <(cd "$1"; find "$1" | sort). -_-

ps: Bookmarked comandlinefu. Really nice site that I don't use often enough. Thanks again tongue

Last edited by Earnestly (2012-03-16 14:25:06)

Offline

#1734 2012-03-16 14:27:11

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

Re: Post your handy self made command line utilities

Kaustic wrote:
skanky wrote:

@Kaustic This is from commandlinefu website, and it should do what you want as it cds to dir first, then does find on . so the path always starts ./foo. It's a bit of a mix between your two - I assume anyway.

diff --suppress-common-lines -y <(cd path-to-dir1; find .|sort) <(cd path-to-dir2; find .|sort)

I tries to run on current dir if the cd fails, so you could change the ; to && avoid that if you type the path wrong.

Perfect, all working now.
I was making the dumb mistake of <(cd "$1"; find "$1" | sort). -_-

ps: Bookmarked comandlinefu. Really nice site that I don't use often enough. Thanks again tongue

No worries, I saw it a while back and thought, that'll come in useful...but so far the only use I've had for it was to post it into this thread. neutral


"...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

#1735 2012-03-16 21:34:44

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Post your handy self made command line utilities

Kaustic wrote:

(even if you cd first)

I'm glad you got this fixed; you can tell I thought you were already using find ./ by the way I said "since each find (or ls) should be relative to the path you cd to" so I probably wouldn't have been much help.

Kaustic wrote:

A very bad workaround for this was to simply use sed to remove the offending directories (e.g. sed 's/\home\/earnest\///').

Though it is still a bad workaround for this case, you should know that sed allows you to use other characters for the delimiter. For instance:

sed "s:^$1::"

to remove the contents of the first argument from the starts of the line (assuming $1 has no colons in it).

Kaustic wrote:

(I've tried a for loop using basename, which is... interesting, lol. It works well enough but using a higher -maxdepth than 1 results in comedy.)

This would make it check to make sure that somewhere in both the arguments there is a file named the same. Which, when going deeper into folders is probably not what you want.



Finally, I'll add for both Kaustic and fsckd's sake that diff can take directories as arguments.

Offline

#1736 2012-03-16 22:04:06

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

jac wrote:
sed "s:^$1::"

to remove the contents of the first argument from the starts of the line (assuming $1 has no colons in it).

That's quite clever, I should learn more about sed and regex, it's just so daunting tongue

jac wrote:

Finally, I'll add for both Kaustic and fsckd's sake that diff can take directories as arguments.

I'm aware of that, but it takes much, much longer rather than supplying diff with a simple stdin. (Which is what lead me on this goose-chase in the first place.)

edit: Sorry I didn't thank you for all your efforts and advice. Cheers smile

Last edited by Earnestly (2012-03-16 22:04:35)

Offline

#1737 2012-03-16 22:53:42

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Post your handy self made command line utilities

Kaustic and jac made me find a better solution than sed or cd.

find "$1" -path "$1" -o -printf '%P\n'

-path "$1" - matches for the path searched and does nothing! that includes not printing the name
-o - this is the or operator, if the -path match fails, try the next test which is match on everything
-printf '%P\n' - here is the magic, this is the action part of match on anything, it prints the file name sans the "$1"

Kaustic, the difference between your script and mine is your's shows differences between the listings of two directory trees and mine shows if the objects in the two trees differ.

My script above, as I said, is a simplified version of the actual script I use. I removed extra things like counters, etc. and only left a framework for which one can add their own stuff. Here is an even more simplified version that eliminates that framework and may make it clearer as to what it's doing,

#!/bin/bash

src="$( find "$1" -path "$1" -o -printf '%P\n' | sort )"
tgt="$( find "$2" -path "$2" -o -printf '%P\n' | sort )"

cat <( comm -23 <( echo "$src" ) <( echo "$tgt" ) | sed 's,^,- ,' ) \
    <( comm -13 <( echo "$src" ) <( echo "$tgt" ) | sed 's,^,+ ,' ) \
    <( comm -12 <( echo "$src" ) <( echo "$tgt" ) | 
          parallel diff -q "$1/{}" "$2/{}" \&\>/dev/null \|\| echo "x {}"  ) \
| sort -k2

Here's some sample output,

> bash dirtreediff2 /tmp/l /tmp/q
+ bar
x baz
+ baz/hi
- foo
> tree /tmp/{l,q}
/tmp/l
├── baz
├── foo
└── same
/tmp/q
├── bar
├── baz
│   └── hi
└── same

Note /tmp/l/baz is a file.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#1738 2012-03-16 23:04:17

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

Writing a script comparing 3 or more directory trees will be left as an exercise for the reader.

Offline

#1739 2012-03-21 09:30:07

Nichollan
Member
From: Stavanger, Norway
Registered: 2010-05-18
Posts: 110

Re: Post your handy self made command line utilities

There are a few C functions that I wish were commands:

daemon.c:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	if(argv[1] == NULL) {
		return 1;
	}
	daemon(1, 0);
	execvp(argv[1], argv + 1);
	return 0;
}

Last edited by Nichollan (2012-03-21 09:30:59)

Offline

#1740 2012-03-21 09:35:58

Avant-texte
Member
Registered: 2012-02-13
Posts: 136

Re: Post your handy self made command line utilities

alias xyzzy='if (( $(date +%s)%42==0 )); then echo "Twice as much happens"; else echo "Nothing happens"; fi;'

tongue

Offline

#1741 2012-03-23 19:27:47

sputnick
Member
From: Near Paris
Registered: 2009-05-27
Posts: 40

Re: Post your handy self made command line utilities

I have this in ~/bin/alsamixer (first in the PATH) to avoid remember strange keyboard keys (my keymap is FR and my keyboard is azerty) to increase/decrease left/right/both channel with the same mnemonic as US keyboard.

man alsamixer wrote:

        You can also control left & right levels for the current channel independently, as follows:

        [Q | W | E ]  -- turn UP [ left | both | right ]

        [Z | X | C ] -- turn DOWN [ left | both | right ]

#!/bin/bash
# map q,w,e and z,x,c at the same place as an US qwerty keyboard for azerty ones for alsamixer

reset() {
    xmodmap -e "keycode 24 = a"
    xmodmap -e "keycode 25 = z"
    xmodmap -e "keycode 52 = w"
}

xmodmap -e "keycode 24 = q"
xmodmap -e "keycode 25 = w"
xmodmap -e "keycode 52 = z"

trap 'reset' 0 1 2 3 15

/usr/bin/alsamixer -D hw:0 "$@"

Offline

#1742 2012-03-23 20:10:43

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: Post your handy self made command line utilities

Todo lists the KISS way:

ndt.sh (inspired by Now Do This)

#!/bin/sh

file=${NDT_FILE:-~/todo}

case $1 in
(".")  sed -i 1d $file     ;;
("")   sed 1q $file        ;;
(*)    echo "$@" >> $file  ;;
esac

Now add your own aliases:

alias h='NDT_FILE=~/home ndt'
alias o='NDT_FILE=~/office ndt'

Usage:

~ $ h Feed the cat
~ $ h Buy the moon
~ $ h
Feed the cat
~ $ h .
~ $ h
Buy the moon

Oh, and if you want to keep a diary, look no further:

#!/bin/sh
${EDITOR:-vi} ~/diary/$(date +"%Y-%m-%d").txt

Offline

#1743 2012-03-24 02:11:16

Piki
Member
From: Cyberspace
Registered: 2012-03-13
Posts: 12

Re: Post your handy self made command line utilities

There are many iptables scripts out there, but most are written as an example. This one is meant more as a "yes or no" script, asking whether or not to enable specific ports.

I wrote this for one of my friend so he could adjust to Linux and the command line basics before actually attempting iptables on his own.

This script includes some common ports. I am willing to accept feedback on what additional ports to enable, just remember that the goal is to include ports relevant to most people in the Linux world, rather than ports that Linux users rarely use. I'll edit this post as needed to add the additional ports.

#!/bin/bash
# simple iptables configuration script
# Changelog:
#	V0.1 [24 Mar 2012]:	Initial Version
#	v0.2 [31 Mar 2012]: 	(1) [FIX] Fixed a variable
#				(2) [FIX] Fixed some typos/grammar in the output
#				(3) [FIX] Removed a few commands that iptables didn't recognize
#				(4) [IMPROVE] Rearranged a few commands for consistency when editing
#				(5) [IMPORVE] Added in NTP
#	v0.3 [11 Apr 2012]	(1) [IMPROVE] Added outgoing ping
#				(2) [IMPROVE] Rewrite some of the output
#	v0.4 [27 May 2012]	(1) [IMPROVE] Rearrange/shorten output
#				(2) [IMPROVE] Added AIM/ICQ port
#				(3) [IMPROVE] Added MSN/Windows Live Messenger port
#	v0.5 [28 May 2012]	(1) [FIX] Add the command needed to enable MSN/WLM
# Author: Kris "Piki" < no public email >
# Alt contact: Freenode IRC (chat.freenode.net) nicks: Piki Piki| Piki|1
# 	^I'm on sporadically during daylight hours Eastern Time

bold=`tput bold`
normal=`tput sgr0`

echo
echo "${bold}Make sure you are logged in as root.${normal}"
echo "Alternatively, you may use sudo."

echo
echo "This script will ask you whether or not to allow some common ports."
echo "As a rule of thumb, ${bold}never${normal} allow more ports than you need. Open ports are how hackers get in!"
echo "It's always a good idea to allow DNS, normal HTTP (web browsing), and secure HTTP."

echo
echo "Type either yes or the letter y in ${bold}lower case${normal}. Anything else and this script will assume no."

echo
echo "Allow DNS lookups? (port 53, TCP and UDP)"
read dns

echo
echo "Allow outgoing ping requests?"
read ping

echo
echo "Allow normal HTTP? (port 80, TCP)"
read http

echo
echo "Allow secure HTTP? (port 443, TCP)"
read https

echo
echo "Allow alternate HTTP? (port 8080, TCP)"
read http_alt

echo "Allow NTP? (port 123, UDP)"
read ntp

echo
echo "Allow normal IMAP? (port 143, TCP)?"
read imap

echo
echo "Allow secure IMAP? (port 993, TCP)?"
read imaps

echo
echo "Allow normal POP3? (port 110, TCP)?"
read pop3

echo
echo "Allow secure POP3? (port 995, TCP)?"
read pop3s

echo
echo "Allow normal SMTP? (port 25, TCP) (some ISPs may block port 25, try 587 or 465)"
read smtp

echo
echo "Allow secure SMTP? (port 587, TCP)"
read smtps

echo
echo "Allow alternate secure SMTP? (port 465, TCP)"
read smtps_alt

echo
echo "Allow AIM/ICQ? (port 5190, TCP and UDP)"?
read aim

echo
echo "Allow MSN/Windows Live Messenger? (port 1863, TCP) (you also need HTTP and secure HTTP enabled)"
read msn

echo
echo "Allow IRC? (port 6667, TCP) (very few IRC networks use a different port)"
read irc

echo
echo "Allow secure IRC? (port 669,TCP)"
read irc_ssl

echo
echo "Allow Jabber/XMPP? (port 5222, TCP) (needed for GTalk)"
read xmpp

echo
echo "Allow alternate Jabber/XMPP? (port 5223, TCP) (rarely used any more)"
read xmpp_alt

echo
echo "Allow ssh to another computer? (port 22, TCP)"
read ssh_out

echo
echo "Allow ssh to this computer? (port 22, TCP)"
read ssh_in

echo
echo "Allow regular torrent ports? (6881-6887, TCP and UDP)"
read torrent

echo
echo "Allow extra torrent ports? (6888-6999, TCP and UDP)"
read torrent_extra

echo
echo "Clearing all existing iptables rules..."
iptables -F OUTPUT
iptables -F INPUT
iptables -F FORWARD
iptables -F -t nat
iptables -F
iptables -X -t nat
iptables -X
iptables -Z OUTPUT
iptables -Z INPUT
iptables -Z FORWARD
iptables -Z -t nat
iptables -Z

echo
echo "Drop all communication, unless explicitly allowed..."
iptables -P OUTPUT DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP

echo
echo "Automtically drop invalid packets..."
iptables -A OUTPUT -p ALL -m state --state INVALID -j DROP
iptables -A INPUT -p ALL -m state --state INVALID -j DROP
iptables -A FORWARD -p ALL -m state --state INVALID -j DROP

echo
echo "Allow unlimited communications on loopback (needed for some local applications)..."
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT

if [ "$dns" == "y" -o "$dns" == "yes" ]; then
echo
echo "Allowing port 53 for DNS lookups..."
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp -m state --state ESTABLISHED,RELATED --sport 53 -j ACCEPT
fi

if [ "$ping" == "y" -o "$ping" == "yes" ]; then
echo
echo "Allow outgoing ping requests..."
iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 0 -m state --state ESTABLISHED,RELATED -j ACCEPT
fi

if [ "$http" == "y" -o "$http" == "yes" ]; then
echo
echo "Allowing port 80 for web browsing..."
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 80 -j ACCEPT
fi

if [ "$https" == "y" -o "$https" == "yes" ]; then
echo
echo "Allowing port 443 for secure web browsing..."
iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 443 -j ACCEPT
fi

if [ "$http_alt" == "y" -o "$http_alt" == "yes" ]; then
echo
echo "Allowing alternate port 8080 for web browsing..."
iptables -A OUTPUT -p tcp --dport 8080 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 8080 -j ACCEPT
fi

if [ "$ntp" == "y" -o "$ntp" == "yes" ]; then
echo
echo "Allowing port 123 for NTP..."
iptables -A OUTPUT -p udp --dport 123 -j ACCEPT
iptables -A INPUT -p udp -m state --state ESTABLISHED,RELATED --sport 123 -j ACCEPT
fi

if [ "$imap" == "y" -o "$imap" == "yes" ]; then
echo
echo "Allowing port 143 for IMAP access..."
iptables -A OUTPUT -p tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 143 -j ACCEPT
fi

if [ "$imaps" == "y" -o "$imaps" == "yes" ]; then
echo
echo "Allowing port 993 for secure IMAP access..."
iptables -A OUTPUT -p tcp --dport 993 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 993 -j ACCEPT
fi

if [ "$smtp" == "y" -o "$smtp" == "yes" ]; then
echo
echo "Allowing port 25 for SMTP access..."
iptables -A OUTPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 25 -j ACCEPT
fi

if [ "$smtps" == "y" -o "$smtps" == "yes" ]; then
echo
echo "Allowing port 587 for secure SMTP access..."
iptables -A OUTPUT -p tcp --dport 587 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 587 -j ACCEPT
fi

if [ "$smtps_alt" == "y" -o "$smtps_alt" == "yes" ]; then
echo
echo "Allowing alternate port 465 for (secure) SMTP access..."
iptables -A OUTPUT -p tcp --dport 465 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 465 -j ACCEPT
fi

if [ "$aim" == "y" -o "$aim" == "yes" ]; then
echo
echo "Allowing port 5190 for AIM and ICQ access..."
iptables -A OUTPUT -p tcp --dport 5190 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 5190 -j ACCEPT
iptables -A OUTPUT -p udp --dport 5190 -j ACCEPT
iptables -A INPUT -p udp -m state --state ESTABLISHED,RELATED --sport 5190 -j ACCEPT
fi

if [ "$msn" == "y" -o "$msn" == "yes" ]; then
echo
echo "Allowing port 1863 for MSN/Windows Live Messnger access..."
iptables -A OUTPUT -p tcp --dport 1863 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 1863
fi

if [ "$irc" == "y" -o "$irc" == "yes" ]; then
echo
echo "Allowing port 6667 for IRC access..."
iptables -A OUTPUT -p tcp --dport 6667 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 6667
fi

if [ "$irc_ssl" == "y" -o "$irc_ssl" == "yes" ]; then
echo
echo "Allowing port 6697 for secure IRC access..."
iptables -A OUTPUT -p tcp --dport 6697 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 6697
fi

if [ "$xmpp" == "y" -o "$xmpp" == "yes" ]; then
echo
echo "Allowing port 5222 for Jabber/XMPP access..."
iptables -A OUTPUT -p tcp --dport 5222 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 5222 -j ACCEPT
fi

if [ "$xmpp_alt" == "y" -o "$xmpp_alt" == "yes" ]; then
echo
echo "Allowing alternative port 5223 for Jabber/XMPP access..."
iptables -A OUTPUT -p tcp --dport 5223 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 5223 -j ACCEPT
fi

if [ "$ssh" == "y" -o "$ssh" == "yes" ]; then
echo
echo "Allowing port 22 for outgoing ssh access..."
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 22 -j ACCEPT
fi

if [ "$ssh" == "y" -o "$ssh" == "yes" ]; then
echo
echo "Allowing port 22 for incoming ssh access..."
iptables -A OUTPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
fi

if [ "$torrent" == "y" -o "$torrent" == "yes" ]; then
echo
echo "Allowing ports 6881 through 6887 for BitTorrent..."
iptables -A OUTPUT -p tcp --dport 6881:6887 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 6881:6887 -j ACCEPT
iptables -A OUTPUT -p udp --dport 6881:6887 -j ACCEPT
iptables -A INPUT -p udp -m state --state ESTABLISHED,RELATED --sport 6881:6887 -j ACCEPT
fi

if [ "$torrent_extra" == "y" -o "$torrent_extra" == "yes" ]; then
echo
echo "Allowing extra ports 6888 through 6999 for BitTorrent access..."
iptables -A OUTPUT -p tcp --dport 6888:6999 -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED --sport 6888:6999 -j ACCEPT
iptables -A OUTPUT -p udp --dport 6888:6999 -j ACCEPT
iptables -A INPUT -p udp -m state --state ESTABLISHED,RELATED --sport 6888:6999 -j ACCEPT
fi

echo
echo
echo "DONE!"

echo
echo
echo "If it didn't work, double check that you are logged in as root, or using sudo."
echo "Also make sure iptables is installed. Some distros don't install it by default."

echo
echo
echo "Just a reminder that if you need more ports, you can edit this script in your"
echo "favorite text editor to add them, using the other lines as an example."
echo "Alternative, you may manually run the iptables commands."

Last edited by Piki (2012-05-28 22:25:34)


--
Piki
"Many people have died for their beliefs. The real courage is in living and suffering for what you believe in." Brom, character in 'Eragon' by Christopher Paolini

Offline

#1744 2012-03-24 18:08:02

TheLemonMan
Member
From: Italy
Registered: 2011-09-04
Posts: 214
Website

Re: Post your handy self made command line utilities

(sleep 5m && echo "Sir, your tea is ready.")&

Earl Gray Tea deserves precision smile

Offline

#1745 2012-03-25 01:05:05

sputnick
Member
From: Near Paris
Registered: 2009-05-27
Posts: 40

Re: Post your handy self made command line utilities

I have plenty of scripts of all kinds tongue :

$ find ~/bin /home/sputnick/repository/{awk,bash,C,greasemonkey,java,javascript,perl,php,prolog,python,ruby,sikuli,windows} -type f | wc -l
2900

If I search only scripts signed by myself :

$ find ~/bin /home/sputnick/repository/{awk,bash,C,greasemonkey,java,javascript,perl,php,prolog,python,ruby,sikuli,windows} -type f -exec bash -c '
    grep -q sputnick "$1" && echo ok
' -- {} \; |
wc -l
542

(some scripts are not my own and some are just a few lines without any signature smile)


The followings scripts are a kind of best off :

google-translate: translate with google for free (use it at your own risk, that probably breaks google TOS). Usefull combined with xclip (X clipboard tools) + zenity and a keyboard combo (I use windows+l to translate English -> French and windows+shift+l to translate French->English) : you just have to select something anywhere and call the script to displays a graphic zenity translated text box.

Usage:
  ./google_translate.py <OPTS>

  -h         | --help           this help
  -f <from>  | --from <from>    original language
  -t <to>    | --to <to>        destination language
  -w <text>  | --words <text>   text
  -v         | --version        google_translate version
  
  Example: 
  ./google_translate.py -f en -t fr -w "A grey hat"

  See http://tinyurl.com/73xljez for all supported languages

See https://github.com/sputnick-dev/google_translate


PacDiff: A pacnew/pacsave bash script that helps to do changes with vimdiff and asking to remove .pac* file if the target file is modified :

#!/bin/bash
# 2012-03-25 03:02:30.0 +0200 / sputnick <gilles <DOT> quenot <AT> gmail>

for file in $(find /boot /etc /var -name '*.pacnew' -o -name '*.pacsave'); do
    sha1=$(sha1sum "${file%.pac*}" | cut -d' ' -f1)
    vimdiff "$file" "${file%.pac*}"
    newsha1=$(sha1sum "${file%.pac*}" | cut -d' ' -f1)
    if [[ $sha1 != $newsha1 ]]; then
        read -p "rm -f $file ? >>> "
        [[ $REPLY == [oOyY]* ]] && rm -f "$file"
    fi
done

TwinSwitch: switch twinview between one or 2 nvidia screens (adapt to your needs)

#!/bin/bash

case $(xrandr | grep "^default" | cut -d" " -f3) in
	1440*) disper -e -t right ;;
	2880*) disper -s ;;
esac 2> >(egrep -v 'unrecognised scaling value|Failed to get size of gamma for output default')

TinyUrl: tinyfy an URL with goo.gl web site API. Uses zenity or CLI mode depending of the context. Usefull with a keyboard combo (I use win+t "t" like tinify), the script search and feed the X clipboard.
(requires curl, xclip, zenity, libnotify)

#!/bin/bash
# 2012-03-20 15:33:55.0 +0100 / sputnick <gilles <DOT> quenot <AT> gmail>

DISPLAY=:0

# are we in a terminal or no ?
if [[ -t 0 ]]; then
    curl -s -d "url=$1" http://goo.gl/api/url | grep -oP 'short_url":"\Khttp://goo.gl/\w+'
else
    url=$(xclip -o)

    if [[ $url != http* ]]; then
        zenity --error --text "URL must starts with http*"
        exit 1
    fi

    out=$(curl -s -d "url=$url" http://goo.gl/api/url | grep -oP 'short_url":"\Khttp://goo.gl/\w+')
    printf '%s' $out | xclip
    notify-send "$out  is in the clipboard..."
fi

screencast: takes a screencast in your selected area in a defined format (mkv here). The file name will be typed in the terminal to save time. The first optional argument is the sleep time before beginning. Type "q" to stop recording.
requires xvkbd, ffmpeg, imagemagick

#!/bin/bash
# 2012-03-14 15:33:55.0 +0100 / sputnick <gilles <DOT> quenot <AT> gmail>
# inspired by screencastor

player=mplayer
codec=libx264 # libx264 = mkv 
frames_in=30
frames_out=25
threads=0 # 0 = auto detectection

# select X area until false
until [[ ${infos[6]} =~ ^[[:digit:]]+$ || ${infos[7]} =~ ^[[:digit:]]+$ ]]; do
    infos=$(import -identify /dev/null) infos=${infos//x/ } infos=(${infos//+/ })
done

((${infos[2]} % 2)) && largeur=$((${infos[2]} + 1)) || largeur=${infos[2]}
((${infos[3]} % 2)) && hauteur=$((${infos[3]} + 1)) || hauteur=${infos[3]}

res="${largeur}x${hauteur}"
pos="${infos[6]},${infos[7]}"

file=/tmp/out_$(date +%Y%m%d%H%M%S.mkv)

sleep ${1:-2}

# ffmpeg [input options] -i [input file] [output options] [output file]
ffmpeg -f x11grab -r $frames_in -s "$res" -i "$DISPLAY+$pos" -vcodec $codec -preset ultrafast -crf 0 -threads $threads -g "250" -keyint_min "25" -trellis "1" -r $frames_out $file

echo $file

# type the file on the terminal to save your time ;)
{ xvkbd -xsendevent -text "$player $file"; } 2> >(grep -Ev 'Warning:|xvkbd:')

AlsaChecklist: my interactive checklist if sound "doesn't work". The checklist is the same as the #alsa IRC channel on freenode network.

#!/bin/bash

amixer set 'Master',0 90%,90% unmute
amixer set 'PCM',0 90%,90% unmute
amixer set 'Front',0 90%,90% unmute

cat  <<-EOF
1) run alsaconf as root
2) add yourself to the 'audio' group (log out and log in again)
3) use alsamixer and unmute channels and raise levels (also try muting some channels) ( amixer set 'Front',0 90%,90% unmute )
4) arts or esound stopped?
5) OSS modules unloaded?
6) speakers on?
7) modprobe snd-pcm-oss
8) does "aplay /usr/share/sounds/alsa/Noise.wav" work for root? Test your sound with aplay and a wav so codec issues don't confuse the situation. <list alsa users>
EOF

coin.bash: a bash joke, that displays a duck and use sounds right from the script itself tongue (ctrl+c to quit wink). This is inspired by makeself command (nvidia use that by example to share drivers)
http://www.sputnick-area.net/scripts/coin.bash

Last edited by sputnick (2012-03-25 10:12:52)

Offline

#1746 2012-03-25 09:25:31

Doomcide
Member
Registered: 2011-08-22
Posts: 221

Re: Post your handy self made command line utilities

^ The google_translate script is great! Thanks a bunch smile

Offline

#1747 2012-03-25 20:26:50

kaari
Member
Registered: 2011-08-12
Posts: 22

Re: Post your handy self made command line utilities

c opens configs. It will open itself if asked to do the impossible.

#! /bin/sh
# opens configs, omits needless thinking
E="vim"
case "$1" in
 dwm)     $E ~/dwm/config.h;;
 mpd)     $E ~/.mpd/mpd.conf;;
 ncmpcpp) $E ~/.ncmpcpp/config;;
 ranger)  $E ~/.config/ranger;;
 *)       $E $0
esac

Offline

#1748 2012-03-25 20:55:02

i_love_penguins
Member
From: Germany
Registered: 2010-03-30
Posts: 46
Website

Re: Post your handy self made command line utilities

I'd like to post yet another weather script, that utilizes the Google API and xmllint to parse Xpath out of the response. You also need PHP which is needed to URL-encode the location.

#!/bin/bash

lang="de"
location="new york"

location=`php -r "echo rawurlencode('$location');"`

tmp=`mktemp`
curl -s "http://www.google.com/ig/api?weather=$location&hl=$lang" > $tmp
recode latin1..UTF-8 $tmp

c=`xmllint --xpath "string(//current_conditions/condition/@data)" $tmp`
t=`xmllint --xpath "string(//current_conditions/temp_c/@data)" $tmp`

echo "$t °C, $c" > ~/.weather

You may edit location and lang to your needs.
I let cron run this script every 10 minutes (to prevent request excess) and then display the ~/.weather via conky.

Offline

#1749 2012-03-25 21:02:29

sputnick
Member
From: Near Paris
Registered: 2009-05-27
Posts: 40

Re: Post your handy self made command line utilities

@i_love_penguins, better use perl instead of php while it's most installed.

perl -pe 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' <<< 'http://google.com/script.php?foo=bar&po=555'
http%3A%2F%2Fgoogle%2Ecom%2Fscript%2Ephp%3Ffoo%3Dbar%26po%3D555%0A

or simply use the

--data-urlencode

curl option wink

Offline

#1750 2012-03-26 11:18:20

whitie
Member
Registered: 2011-03-13
Posts: 21

Re: Post your handy self made command line utilities

Here is a version of google-translate, which uses Arch standard Python (Python 3.2) and only the standardlib. No third party packages needed.

See https://bitbucket.org/whitie/container/ … /gtrans.py

Whitie

Last edited by whitie (2012-03-26 18:03:21)

Offline

Board footer

Powered by FluxBB