Thursday, February 21, 2013

Comparing directories

Here's how to compare the filenames (not the content of the files!) of two directories:
rsync -nav --delete dir1/ dir2/

NOTE! Don't forget the -n option, otherwise the files from dir1 will be copied to dir2!

The slash ('/') after the directory names is necessary, otherwise it will also check for dir1 resp. dir2 in the path name.

This command will output all files from dir1 which don't exist in dir2. Files only existing in dir2 will be prefixed with 'deleting', because of the delete option.

To check also for differences in the file's contents, use diff:
diff -qr dir1 dir2

The -q option omits the output of the actual difference in the files and provides a better readable output. -r scans directories recursively.

Tuesday, February 12, 2013

Powershell: get Event Log Entries of the last 24 Hours

To get the EventLog Entries, we can use the Get-EventLog or Get-WMIObject Commandlet. In this case I'm using Get-Eventlog just because it's faster. Get-WMIObject may be used to get logs from remote machines.

Monday, February 11, 2013

One-Liner: Connect to a WPA Secured WIFI-Network from the command line

Just replace PASSPHRASE, ESSID and maybe wlan0 with your real network interface name.
to scan for available networks, use
iwlist wlan0 scan | grep -i essid

Watch out! Passphrase is visible on command line and probably in the bash history!
sudo ifconfig wlan0 up ; echo 'PASSPHRASE' | wpa_passphrase 'ESSID' | sudo wpa_supplicant  -iwlan0 -c /dev/stdin > /tmp/wpa_supplicant.log 2>&1 & sudo dhclient wlan0 &

 

Friday, February 8, 2013

Perl File Test Operators

OperatorDescription
-A FILENAMEReturn the access age of FILENAME
-b FILENAMETests if FILENAME is a block device
-B FILENAMETests if FILENAME is a binary file. Can also be applied to a file handle!
-c FILENAMETests if FILENAME is a character device
-C FILENAMEReturn the inode change age of FILENAME
-A FILENAMEReturn the access age of FILENAME
-d FILENAMETests if FILENAME is a directory
-e FILENAMETests if FILENAME exists
-f FILENAMETests if FILENAME is a regular file, not, e.g., a directory
-g FILENAMETests if FILENAME has the setgid bit
-k FILENAMETests if FILENAME has the sticky bit
-l FILENAMETests if FILENAME is a symbolic link
-M FILENAMEReturns age of FILENAME
-o FILENAMETests if FILENAME is owned by the current UID
-p FILENAMETests if FILENAME is a named pipe
-r FILENAMETests if FILENAME is readable
-s FILENAMEReturns size of FILENAME
-S FILENAMETests if FILENAME is a socket
-t FILENAMETests if FILENAME is opened to a tty
-T FILENAMETests if FILENAME is a text file
-u FILENAMETests if FILENAME has the setuid bit
-w FILENAMETests if FILENAME is writable
-x FILENAMETests if FILENAME is executable
-S FILENAMETests if FILENAME is a socket
-z FILENAMETests if size of FILENAME is zero

Vi Cheat Sheet (buffers)

opening buffers:

:badd file.name # open buffer in background

listing buffers:

:buffers # list buffers
:files # list buffers
:ls! # list buffers

closing buffers:

:bdelete file.name # close buffer containing file.name
:bdelete 1 # close buffer 1
:bd # close current buffer
:3,5 bdelete # delete buffers 3-5
:bdelete file1 file2 # ...

splitting:

:sbuffer file/num # split window, put buffer in new frame

navigation:

:bfirst / :brewind # to first buffer
:sbfirst # first buffer in new window
:sblast # ...
:sbnext / sbprevious
:bp # abbr. bprevious
:ball #
:sball
:unhide
:sunhide
:help buffers-menu
:help hidden

create a scratch (temp) buffer:

:set buftype=nofile
:set bufhidden=hide
:setlocal noswapfile
:set nobuflisted # hide in buffer list
:set modified? # is buffer modified?
:set modified # set buffer expl. as modified
:set nomodifiable # create read-only buffer
:set modifiable

some random IPv6 notes

resources:
http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/index.html
Silvia Hagen: IPv6 Essentials (O'Reilly, 2006, ISBN 0-596-10058-2)
David Malone, Niall Murphy: IPv6 Network Administration (O'Reilly, 2005, ISBN 0-596-00934-8)

invalid argument ?:
# ping6 fe80::212:34ff:fe12:3456
connect: Invalid argument
the kernel doen't know through which interface to send. specify the interface with -I
# ping6 -I eth0 fe80::2e0:18ff:fe90:9205
PING fe80::212:23ff:fe12:3456(fe80::212:23ff:fe12:3456) from
¬ fe80::212:34ff:fe12:3478 eth0: 56 data bytes
64 bytes from fe80::212:23ff:fe12:3456: icmp_seq=0 hops=64 time=445 usec

 ping6 to the link-local all-node multicast address to find ipv6 activated hosts in the network:
panda@marvin ~ $ ping6 -I eth0 ff02::1
PING ff02::1(ff02::1) from fe80::92e6:baff:fec1:7749 eth0: 56 data bytes
64 bytes from fe80::92e6:baff:fec1:7749: icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from fe80::222:5fff:fe03:3225: icmp_seq=1 ttl=64 time=127 ms (DUP!)
64 bytes from fe80::52ea:d6ff:fe92:a40e: icmp_seq=1 ttl=64 time=133 ms (DUP!)
64 bytes from fe80::92e6:baff:fec1:7749: icmp_seq=2 ttl=64 time=0.066 ms
64 bytes from fe80::222:5fff:fe03:3225: icmp_seq=2 ttl=64 time=45.9 ms (DUP!)
64 bytes from fe80::52ea:d6ff:fe92:a40e: icmp_seq=2 ttl=64 time=57.2 ms (DUP!)
^C
--- ff02::1 ping statistics ---
2 packets transmitted, 2 received, +4 duplicates, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.056/60.810/133.580/53.861 ms

regex for address, sort, uniq and you have all ipv6 active hosts! (which answered during 5 seconds)
 $ ping6 -w5 -I eth0 ff02::1 | egrep -o '([0-9a-f]+::?){4}[0-9a-f]+' | sort | uniq
fe80::222:5fff:fe03:3225
fe80::223:26ff:fe3c:1d35

 

List of Android Key Codes

fyi

[code]
0 getAction() value: the key has been pressed down

0 Unknown key code

1 getAction() value: the key has been released

1 Soft Left key
Usually situated below the display on phones and used as a multi-function feature key for selecting a software defined function shown on the bottom left of the display.

1 This mask is set if the device woke because of this key event

2 getAction() value: multiple duplicate key events have occurred in a row, or a complex string is being delivered
If the key code is not {#link KEYCODE_UNKNOWN then the {#link getRepeatCount() method returns the number of times the given key code should be executed. Otherwise, if the key code is KEYCODE_UNKNOWN, then this is a sequence of characters as returned by getCharacters().

2 Soft Right key
Usually situated below the display on phones and used as a multi-function feature key for selecting a software defined function shown on the bottom right of the display.

2 This mask is set if the key event was generated by a software keyboard

3 Home key
This key is handled by the framework and is never delivered to applications.

4 Back key

4 This mask is set if we don't want the key event to cause us to leave touch mode

5 Call key

6 End Call key

7 '0' key

8 '1' key

8 This mask is set if an event was known to come from a trusted part of the system
That is, the event is known to come from the user, and could not have been spoofed by a third party component.

9 '2' key

10 '3' key

11 '4' key

12 '5' key

13 '6' key

14 '7' key

15 '8' key

16 '9' key

16 This mask is used for compatibility, to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"
This allows TextView to dispatch these as normal enter keys for old applications, but still do the appropriate action when receiving them.

17 '*' key

18 '#' key

19 Directional Pad Up key
May also be synthesized from trackball motions.

20 Directional Pad Down key
May also be synthesized from trackball motions.

21 Directional Pad Left key
May also be synthesized from trackball motions.

22 Directional Pad Right key
May also be synthesized from trackball motions.

23 Directional Pad Center key
May also be synthesized from trackball motions.

24 Volume Up key
Adjusts the speaker volume up.

25 Volume Down key
Adjusts the speaker volume down.

26 Power key

27 Camera key
Used to launch a camera application or take pictures.

28 Clear key

29 'A' key

30 'B' key

31 'C' key

32 'D' key

32 When associated with up key events, this indicates that the key press has been canceled
Typically this is used with virtual touch screen keys, where the user can slide from the virtual key area on to the display: in that case, the application will receive a canceled up event and should not perform the action normally associated with the key. Note that for this to work, the application can not perform an action for a key until it receives an up or the long press timeout has expired.

33 'E' key

34 'F' key

35 'G' key

36 'H' key

37 'I' key

38 'J' key

39 'K' key

40 'L' key

41 'M' key

42 'N' key

43 'O' key

44 'P' key

45 'Q' key

46 'R' key

47 'S' key

48 'T' key

49 'U' key

50 'V' key

51 'W' key

52 'X' key

53 'Y' key

54 'Z' key

55 ',' key

56 '
' key.

57 Left Alt modifier key

58 Right Alt modifier key

59 Left Shift modifier key

60 Right Shift modifier key

61 Tab key

62 Space key

63 Symbol modifier key
Used to enter alternate symbols.

64 Explorer special function key
Used to launch a browser application.

64 This key event was generated by a virtual (on-screen) hard key area
Typically this is an area of the touchscreen, outside of the regular display, dedicated to "hardware" buttons.

65 Envelope special function key
Used to launch a mail application.

66 Enter key

67 Backspace key
Deletes characters before the insertion point, unlike KEYCODE_FORWARD_DEL.

68 '`' (backtick) key

69 '-'

70 '=' key

71 '[' key

72 ']' key

73 '' key

74 ';' key

75 ''' (apostrophe) key

76 '/' key

77 '@' key

78 Number modifier key
Used to enter numeric symbols. This key is not Num Lock; it is more like KEYCODE_ALT_LEFT and is interpreted as an ALT key by MetaKeyKeyListener.

79 Headset Hook key
Used to hang up calls and stop media.

80 Camera Focus key
Used to focus the camera.

81 '+' key

82 Menu key

83 Notification key

84 Search key

85 Play/Pause media key

86 Stop media key

87 Play Next media key

88 Play Previous media key

89 Rewind media key

90 Fast Forward media key

91 Mute key
Mutes the microphone, unlike KEYCODE_VOLUME_MUTE.

92 Page Up key

93 Page Down key

94 Picture Symbols modifier key
Used to switch symbol sets (Emoji, Kao-moji).

95 Switch Charset modifier key
Used to switch character sets (Kanji, Katakana).

96 A Button key
On a game controller, the A button should be either the button labeled A or the first button on the upper row of controller buttons.

97 B Button key
On a game controller, the B button should be either the button labeled B or the second button on the upper row of controller buttons.

98 C Button key
On a game controller, the C button should be either the button labeled C or the third button on the upper row of controller buttons.

99 X Button key
On a game controller, the X button should be either the button labeled X or the first button on the lower row of controller buttons.

100 Y Button key
On a game controller, the Y button should be either the button labeled Y or the second button on the lower row of controller buttons.

101 Z Button key
On a game controller, the Z button should be either the button labeled Z or the third button on the lower row of controller buttons.

102 L1 Button key
On a game controller, the L1 button should be either the button labeled L1 (or L) or the top left trigger button.

103 R1 Button key
On a game controller, the R1 button should be either the button labeled R1 (or R) or the top right trigger button.

104 L2 Button key
On a game controller, the L2 button should be either the button labeled L2 or the bottom left trigger button.

105 R2 Button key
On a game controller, the R2 button should be either the button labeled R2 or the bottom right trigger button.

106 Left Thumb Button key
On a game controller, the left thumb button indicates that the left (or only) joystick is pressed.

107 Right Thumb Button key
On a game controller, the right thumb button indicates that the right joystick is pressed.

108 Start Button key
On a game controller, the button labeled Start.

109 Select Button key
On a game controller, the button labeled Select.

110 Mode Button key
On a game controller, the button labeled Mode.

111 Escape key

112 Forward Delete key
Deletes characters ahead of the insertion point, unlike KEYCODE_DEL.

113 Left Control modifier key

114 Right Control modifier key

115 Caps Lock key

116 Scroll Lock key

117 Left Meta modifier key

118 Right Meta modifier key

119 Function modifier key

120 System Request / Print Screen key

121 Break / Pause key

122 Home Movement key
Used for scrolling or moving the cursor around to the start of a line or to the top of a list.

123 End Movement key
Used for scrolling or moving the cursor around to the end of a line or to the bottom of a list.

124 Insert key
Toggles insert / overwrite edit mode.

125 Forward key
Navigates forward in the history stack. Complement of KEYCODE_BACK.

126 Play media key

127 Pause media key

128 Close media key
May be used to close a CD tray, for example.

128 This flag is set for the first key repeat that occurs after the long press timeout

129 Eject media key
May be used to eject a CD tray, for example.

130 Record media key

131 F1 key

132 F2 key

133 F3 key

134 F4 key

135 F5 key

136 F6 key

137 F7 key

138 F8 key

139 F9 key

140 F10 key

141 F11 key

142 F12 key

143 Num Lock key
This is the Num Lock key; it is different from KEYCODE_NUM. This key alters the behavior of other keys on the numeric keypad.

144 Numeric keypad '0' key

145 Numeric keypad '1' key

146 Numeric keypad '2' key

147 Numeric keypad '3' key

148 Numeric keypad '4' key

149 Numeric keypad '5' key

150 Numeric keypad '6' key

151 Numeric keypad '7' key

152 Numeric keypad '8' key

153 Numeric keypad '9' key

154 Numeric keypad '/' key (for division)

155 Numeric keypad '*' key (for multiplication)

156 Numeric keypad '-' key (for subtraction)

157 Numeric keypad '+' key (for addition)

158 Numeric keypad '
' key (for decimals or digit grouping).

159 Numeric keypad ',' key (for decimals or digit grouping)

160 Numeric keypad Enter key

161 Numeric keypad '=' key

162 Numeric keypad '(' key

163 Numeric keypad ')' key

164 Volume Mute key
Mutes the speaker, unlike KEYCODE_MUTE. This key should normally be implemented as a toggle such that the first press mutes the speaker and the second press restores the original volume.

165 Info key
Common on TV remotes to show additional information related to what is currently being viewed.

166 Channel up key
On TV remotes, increments the television channel.

167 Channel down key
On TV remotes, decrements the television channel.

168 Zoom in key

169 Zoom out key

170 TV key
On TV remotes, switches to viewing live TV.

171 Window key
On TV remotes, toggles picture-in-picture mode or other windowing functions.

172 Guide key
On TV remotes, shows a programming guide.

173 DVR key
On some TV remotes, switches to a DVR mode for recorded shows.

174 Bookmark key
On some TV remotes, bookmarks content or web pages.

175 Toggle captions key
Switches the mode for closed-captioning text, for example during television shows.

176 Settings key
Starts the system settings activity.

177 TV power key
On TV remotes, toggles the power on a television screen.

178 TV input key
On TV remotes, switches the input on a television screen.

179 Set-top-box power key
On TV remotes, toggles the power on an external Set-top-box.

180 Set-top-box input key
On TV remotes, switches the input mode on an external Set-top-box.

181 A/V Receiver power key
On TV remotes, toggles the power on an external A/V Receiver.

182 A/V Receiver input key
On TV remotes, switches the input mode on an external A/V Receiver.

183 Red "programmable" key
On TV remotes, acts as a contextual/programmable key.

184 Green "programmable" key
On TV remotes, actsas a contextual/programmable key.

185 Yellow "programmable" key
On TV remotes, acts as a contextual/programmable key.

186 Blue "programmable" key
On TV remotes, acts as a contextual/programmable key.

187 App switch key
Should bring up the application switcher dialog.

188 Generic Game Pad Button #1

189 Generic Game Pad Button #2

190 Generic Game Pad Button #3

191 Generic Game Pad Button #4

192 Generic Game Pad Button #5

193 Generic Game Pad Button #6

194 Generic Game Pad Button #7

195 Generic Game Pad Button #8

196 Generic Game Pad Button #9

197 Generic Game Pad Button #10

198 Generic Game Pad Button #11

199 Generic Game Pad Button #12

200 Generic Game Pad Button #13

201 Generic Game Pad Button #14

202 Generic Game Pad Button #15

203 Generic Game Pad Button #16

204 Language Switch key
Toggles the current input language such as switching between English and Japanese on a QWERTY keyboard. On some devices, the same function may be performed by pressing Shift+Spacebar.

205 Manner Mode key
Toggles silent or vibrate mode on and off to make the device behave more politely in certain settings such as on a crowded train. On some devices, the key may only operate when long-pressed.

206 3D Mode key
Toggles the display between 2D and 3D mode.

207 Contacts special function key
Used to launch an address book application.

208 Calendar special function key
Used to launch a calendar application.

209 Music special function key
Used to launch a music player application.

210 Calculator special function key
Used to launch a calculator application.

211 Japanese full-width / half-width key

212 Japanese alphanumeric key

213 Japanese non-conversion key

214 Japanese conversion key

215 Japanese katakana / hiragana key

216 Japanese Yen key

217 Japanese Ro key

218 Japanese kana key

219 Assist key
Launches the global assist activity. Not delivered to applications.

256 Set when a key event has FLAG_CANCELED set because a long press action was executed while it was down

512 Set for ACTION_UP when this event's key code is still being tracked from its initial down
That is, somebody requested that tracking started on the key down and a long press has not caused the tracking to be canceled.

1024 Set when a key event has been synthesized to implement default behavior for an event that the application did not handle
Fallback key events are generated by unhandled trackball motions (to emulate a directional keypad) and by certain unhandled key presses that are declared in the key map (such as special function numeric keypad keys when numlock is off).
[/code]

Get MAC-Address of default gateway

I used this to find out which router was used as the default gateway. It's very simple and may need some tweaking, but it worked for my needs.
#! /usr/bin/perl -w

use strict;
use warnings;

my $route = `ip route`;
$route =~ /^default via ((\d+\.?){4})/;

my $router_ip = $1 if $1;

die "unknown\n" unless $router_ip;

my $arp = `cat /proc/net/arp | grep $router_ip`;

my ($ip, $type, $flags, $mac, $mask, $device) = split /\s+/, $arp;

print $mac."\n";

 

 

Simple Multi-Monitor Setup script

This small script uses xrandr to set up multiple monitors.

Usage:
setmonitor <left|right|off>

change the variable values in the beginning of the script according to your monitor setup.

xrandr shows you available monitors and modes.
#! /bin/bash

vga=VGA1
lvds=LVDS1
vgamode=1280x1024
lvdsmode=1680x1050

action=$1

test -n "$action" || exit 1

if [ $action == 'left' ]; then
xrandr --output $vga --mode $vgamode
xrandr --output $lvds --mode $lvdsmode
xrandr --output $vga --left-of $lvds
xrandr --output $vga --auto
elif [ $action == 'right' ]; then
xrandr --output $vga --mode $vgamode
xrandr --output $lvds --mode $lvdsmode
xrandr --output $vga --right-of $lvds
xrandr --output $vga --auto
elif [ $action == 'off' ]; then
xrandr --output $vga --off
else
echo "action $action unknown. Use left | right | off."
fi

 

Screenshot

import -window root -display :0.0 -screen png:- > screenshot.png

The following worked fine for me to download a screenshot via ssh and display it in a loop. Perfect to "spy" out people or give remote support. Needs 'eog' and ssh!

use
xhost +hostname

on the remote host, otherwise the x-server may reject your connection!
#! /usr/bin/perl -w

my $m = shift;

my $filetype= shift || "jpg";

system('eog -w &');
while (1) {
next_pic();
exit unless $m;
}

sub next_pic {
my $temp=`tempfile -s.$filetype`;
`ssh user\@hostname import -window root -display :0.0 -screen $filetype:- > $temp`;
`eog -w $temp > /dev/null 2>&1`;
unlink $temp;
}

 

Generate a random string

takes one argument: length of string (default = 32)
#! /usr/bin/perl -w

use strict;
use warnings;

my $length = shift || 32;
my $customchars = shift;

my @chars;
@chars = split(//, $customchars) if defined($customchars);
@chars = ('a'..'z','A'..'Z','0'..'9','_') unless defined($customchars);

my $string;
foreach (1..$length) {
$string .= $chars[rand @chars];
}

print $string."\n";

 

Convert Unix Epoch to local time string

really simple...
#! /usr/bin/perl -w

print scalar localtime(shift || time) ."\n";

 

convert newline: dos to unix

#! /bin/bash

# converts dos/win line endings to unix

in=$1
out=$2

same=0

cmd=`basename $0`

if [ ! "$in" ]; then
echo "$cmd: convert dos/win line breaks to unix"
echo " Usage: $cmd INFILE [OUTFILE]"
exit 1
fi

if [ "$in" = "$out" ]; then
same=1
out=`tempfile`
fi

tr -d '\015' < "$in" > "$out"

if [ "$same" -eq 1 ]; then
fi