Thursday, November 27, 2008

Awk pattern matching over a particular field


1 #!/bin/bash
2
3 echo babbbbil 12223 | awk '{for(i=1;i<=NF;i++){if($i ~ "122"){print $i;i=NF+1}}}'
4

This piece of awk code will go through all the fields and look for pattern "122" and print them. The "i=NF+1" will work like "break;" in C which will make awk to stop printing after the first hit. Happy Awk'ing ;)


Controlling netgear ( D834G ) router from shell


You can perform the following operations from the shell
  • Disconnect
  • Connect
  • Reconnect (Disconnect, then connect again)
  • Show status
  • reboot router



1 #!/bin/bash
2
3 router_ip=192.168.0.1
4 username="admin"
5 password="CHANGE-ME"
6
7 debug_url="http://$router_ip/setup.cgi?todo=debug"
8 status_url="http://$router_ip/setup.cgi?next_file=s_status.htm"
9 connection_url="http://$router_ip/setup.cgi?next_file=st_poe.htm"
10 disconnect_url="http://$router_ip/setup.cgi?todo=disconnect&next_file=st_poe.htm"
11 connect_url="http://$router_ip/setup.cgi?todo=connect&next_file=st_poe.htm"
12
13
14 RED='\e[1;31m'
15 CYAN='\e[1;36m'
16 NC='\e[0m' # No Color
17
18 tmp_file="/tmp/babil-router.info"
19 sleep_time=3
20
21
22 function clean_up()
23 {
24 rm -f $tmp_file
25 }
26
27 function reboot_router()
28 {
29 report=`wget -q --user $username --password $password $debug_url -O /dev/null`
30 echo "[+] $report"
31 sleep 2
32 (sleep 2; echo "reboot"; sleep 2) | telnet $router_ip
33 }
34
35
36 function ip()
37 {
38 #curl --basic --user $username:$password $status_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
39 wget -q --user $username --password $password $status_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
40
41 c=1
42 while read line
43 do
44 #echo "$c $line"
45 if [[ $c == 71 ]];then
46 echo -e "[+] outbound ip : ${RED} $line ${NC}"
47
48 elif [[ $c == 83 ]];then
49 echo -e "[+] gateway ip : ${RED} $line ${NC}"
50
51 fi
52
53 c=$((c+1))
54
55 done < $tmp_file
56 clean_up
57 }
58
59
60 function stat()
61 {
62
63 ip
64
65 #curl --basic --user $username:$password $connection_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
66 wget -q --user $username --password $password $connection_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
67
68 c=1
69 while read line
70 do
71 #echo "$c $line"
72 if [[ $c == 61 ]];then
73 echo -e "[+] duration : ${RED} $line ${NC}"
74
75 elif [[ $c == 65 ]];then
76 echo -e "[+] status : ${RED} $line ${NC}"
77
78 fi
79
80 c=$((c+1))
81
82 done < $tmp_file
83
84 clean_up
85
86 }
87
88
89 function connect()
90 {
91 echo -n "[+] connecting "
92 #curl --basic --user $username:$password $connect_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
93 wget -q --user $username --password $password $connect_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
94 for ((i=0;i<$sleep_time;i++))
95 do
96 echo -n "."
97 sleep 1
98 done
99
100 echo
101 stat
102 }
103
104
105 function disconnect()
106 {
107 echo -n "[+] disconnecting "
108 #curl --basic --user $username:$password $disconnect_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
109 wget -q --user $username --password $password $disconnect_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
110 for ((i=0;i<$sleep_time;i++))
111 do
112 echo -n "."
113 sleep 1
114 done
115
116 echo
117 stat
118 }
119
120 case "$1" in
121
122 connect)
123 echo
124 connect
125 echo
126 ;;
127
128 disconnect)
129 echo
130 disconnect
131 echo
132 ;;
133
134 stat)
135 echo
136 stat
137 echo
138 ;;
139 reboot)
140 echo
141 reboot_router
142 echo
143 ;;
144 reconnect)
145 echo
146 connect
147 echo
148 disconnect
149 echo
150 ;;
151 *)
152 echo
153 echo -e "[+] usage: `basename $0` { ${RED}connect | disconnect | reconnect | stat | reboot${NC} }"
154 echo
155 exit 1
156 ;;
157
158 esac
159
160

Saturday, November 22, 2008

Ubuntu (intrepid) - skype blocking audio device


After performing a very starnge (and stupid) migration from kubuntu-intrepid to edubuntu-intrepid, then to xbunbu-intrepid and lastly to original Ubuntu-intrepid, my skype sound was blocking all other audio softwares from using my sound card while in a skype-call (basically no software mixing).

I seem to have found two solutions for the problem after suffering and searching for almost a day:

solution-1:: making sure you have pulse-audio installed [1]

  • backup previous configuration files
$ mkdir ~/pulse-backup && cp -r ~/.pulse ~/.asound* /etc/asound.conf /etc/pulse -t ~/pulse-backup/
$ sudo rm -r ~/.pulse ~/.asound* /etc/asound.conf


  • install pulse and adobe flash
$ sudo apt-get install libasound2-plugins padevchooser libao-pulse libsdl1.2debian-pulseaudio flashplugin-nonfree; apt-get remove --purge libflashsupport flashplugin-nonfree-extrasound



solution-2:: create a dummy "skype" device for alsa [2]

  • copy and paste the folling code in ~/.asoundrc and restart skype
======================================================================
pcm.skype {
type asym
playback.pcm "skypeout"
capture.pcm "skypein"
}

pcm.skypein {
# Convert from 8-bit unsigned mono (default format set by aoss when
# /dev/dsp is opened) to 16-bit signed stereo (expected by dsnoop)
#
# We can't just use a "plug" plugin because although the open will
# succeed, the buffer sizes will be wrong and we'll hear no sound at
# all.
type route
slave {
pcm "skypedsnoop"
format S16_LE
}
ttable {
0 {0 0.5}
1 {0 0.5}
}
}

pcm.skypeout {
# Just pass this on to the system dmix
type plug
slave {
pcm "dmix"
}
}

pcm.skypedsnoop {
type dsnoop
ipc_key 1133
slave {
# "Magic" buffer values to get skype audio to work
# If these are not set, opening /dev/dsp succeeds but no sound
# will be heard. According to the alsa developers this is due
# to skype abusing the OSS API.
pcm "hw:0,0"
period_size 256
periods 16
buffer_size 16384
}
bindings {
0 0
}
}
======================================================================



[1] http://ubuntuforums.org/showthread.php?t=789578&highlight=pulseaudio+perfect
[2] http://ubuntuforums.org/showthread.php?t=44753&page=15