Real life exampleSaturday, February 11. 2012
I never found the need to study those common unix utilities like sed, awk, xargs. Even my knowledge of grep didn't go further than blah | grep foo. Until yesterday, when I needed to accomplish a task that just happened to involve all of these utilities:
grep ppp /proc/net/dev | sed "s/:/ /" | awk '($2+$10>1000000000){print $1}' | xargs --max-args=1 --no-run-if-empty ppp-off First, grep filters all lines from /proc/net/dev that contain ppp. Then, sed changes the colon into a space using the regex s/:/ /. Next, awk parses the values of the 2nd and 10th column [bytes sent and received] and if the sum exceeds 1GB it prints the value in the first column [the interface]. Finally, xargs invokes ppp-off for every interface returned. Why I needed this is left as an exercise to the reader. Modern COM Programming in DTuesday, January 24. 2012
I finally got around to asking for permission to share the slides of a tech talk I did more than a year ago: Modern COM Programming in D
In the slides I explain how I've made a projection for COM into D, which allows you to use COM objects without the usual hassle of refcount/QueryInterface/HRESULT/BSTR/etc... Sharing the code will be hard (mostly because of the shape it's in) but the slides basically contain all you need. Actually, D has progressed a lot since, so a rewrite might be in order anyway. UPDATE: Add your comments below or on Reddit. import std.stdio, comxml;
void main()
{
// Create a new empty document
auto doc = XmlDocument();
// Load an XML document from an inline string
doc.LoadXml("
Server-side email filteringTuesday, January 24. 2012
I hate etc/ and everything in it, but I realize it might not be etc/'s fault. The fault might lie with Google and Bing: most of the results I get are people asking the same question on some forum. These threads often end without any answer. Or worse, wrong information. About half the threads eventually end up with the members discussing a complete different issue. Anyway, I figured it out. The following example assumes you're using the maildir format for your mailbox.
As you can see, the rules in .procmailrc use regular expressions to find the right mail. Each rule starts with colon-zero-colon (see man procmailrc for more options), followed by a line with the regular expression. Here, ^ means beginning of the line. Note that dots must be escaped with a backslash. A wildcard would be written as .* The next line describes what to do when the regular expression is matched. In this case we want procmail to deliver the mail to a folder called SomeFolder. (If you're using mbox instead of maildir this would be a filename rather than a folder name.) Add as many rules as you want, separating them with an empty line. OpenWRT on Netgear WNDR3700, IPv6 edition!Wednesday, June 8. 2011
Happy IPv6 day everyone!
Here's how I went about it:
FaceboocSaturday, February 19. 2011
Nice IP address they got, by the way:
TCP [2002:74e2:70fd:e472::3]:3405 [2620:0:1cfe:face:b00c::3]:443 ESTABLISHED New sport: extreme hot-pluggingSaturday, February 19. 2011
I was bored so I tried pulling out the harddisk of my laptop while it was running. Windows (7) kept running and didn't crash. Of course, I couldn't actually open any programs, but it didn't lock up. I put the harddisk back in and after a while the programs that I had tried to open all opened up at once. Pretty impressive
I tried to same trick on Ubuntu 10.4. As soon as I pulled out the harddisk, the gnome UI died. The taskbars disappeared. My console window stayed open however, and 'top' (started before I pulled the HDD out) kept running as well. Once I exited 'top' the system was pretty useless, of course. I put the HDD back in, but that didn't do much. Ubuntu didn't seem to be able to recover the way Windows did. I also was unable to login from a new tty. Shouting Winamp to IcecastSunday, February 13. 2011
Did I mention how great the Squeezebox Boom sounds? Well, it sounds great.
I have bought Shukar Collective's latest album, Rromatek, on Amazon today. (Amazon MP3s can only be bought from the US; nothing a VPN can't solve.) Once the download finished I listened to the CD on my laptop, but my Dell has terrible speakers. I immediately wished I could listen to the CD from the Squeezebox instead. The solution I came up with was to hook my Winamp up to the Icecast server on my NAS. Turns out that's harder than you'd think. There's a Winamp DSP plugin that does just that (Oddcast aka Edcast), but it was abandoned, apparently because of "legal reasons". Luckily, Icecast now accepts Shoutcast clients, but the protocol used by Icecast is not the same as the one from Shoutcast and the Shoutcast Source Plugin (get it here) doesn't understand Icecast. Fortunately, since version 2.2, Icecast can handle Shoutcast sources, by adding a single line to your <listen-socket> section in icecast.xml: <shoutcast-mount>/strm<shoutcast-mount>I wanted a new mount for my Winamp, not to interfere with the one from Ices, so it became:
<listen-socket>
<port>8000</port>
<shoutcast-mount>/winamp</shoutcast-mount>
</listen-socket>
<mount>
<mount-name>/winamp</mount-name>
</mount>
In Winamp, go to Preferences, Plugins, DSP/Effects and configure the SHOUTcast Source DSP. Check "Use SHOUTcast v1 mode (for legacy servers)". Disable "Make this server public" on the Yellowpages tab. Select an audio format on the Encoder tab. Press connect, and we're done. Squeezebox Boom + Icecast = WinThursday, February 10. 2011
Been to Hong Kong for Chinese New Year and one of the gadgets I bought there was a Logitech Squeezebox Boom (1880HK$). I had heard about the Squeezebox line before, but had never actually seen one. I have looked on taobao.com but there were only few sellers and (hence?) more expensive than abroad. (There's one seller that seems to have quite a supply and selling them at half the market price, but that's just too shady.)
After the initial setup I was shown the main menu. Because I was using mysqueezebox.com (as opposed to my own server) I could only choose "Internet Radio". However, it was a very extensive list of well known radio stations, and in about 10 minutes I was listening to Studio Brussels! The quality was really good, and definitely better than the Creative D100 speakers I had bought 1 month earlier. But still no access to my own music collection. Second surprise: the Squeezebox does not understand UPnP or DLNA. I thought that was a major bummer. It would have allowed me to simply hook it up to my Buffalo LinkStation Live's Twinkymedia Server. It seemed I had to install a Squeezebox Server. Squeezebox Server was installable on my Linkstation using: ipkg install squeezecenterThat went well (albeit it was a somewhat older version), but it was immediately apparent that the 64Mb RAM of the Linkstation were not enough for the hefty perl + mysql Squeezecenter. The NAS was constantly swapping and became unresponsive. What I basically wanted was simple: when I still had my original Xbox with XBMC, I had made a startup script that immediately started Party Mode as soon as the XBox was turned on. Filling the room with music was a simple act of pressing a button. Most of the time I didn't care about which CD was playing. (Without Party Mode I would spend minutes figuring out what music I felt like listening and usually ending up with one of my favorites, never choosing 90% of the music I actually had.) I had heard about Icecast before. It's a music (media, to be more exact) streamer, similar to shoutcast. It was easily installed on my NAS using ipkg install icecast. Icecast itself is just the streaming server, it doesn't actually read music itself, but needs a Icecast Source to provide it with the bits. That job is done by Ices, also installable using ipkg install Ices0(Apparently nobody has built Ices2 for the ARM yet?) The input for Ices is a text file with the playlist. Because Ices0 (as opposed to Ices2) only understand MP3, I had to make a playlist with only my MP3s, skipping Flac and Ogg: find /mnt/disk1/share/music -name \*.mp3 > playlist.txt Icecast and Ices could now be started with: /opt/bin/icecast -c /opt/etc/icecast.xml -bI could now connect to my own radio station from Winamp! I then proceeded to add the URL to my Favorites on mysqueezebox.com but it wouldn't work. The Squeezebox cannot play streams on the LAN because the server (and in this case, mysqueezebox.com) must be able to access the stream, for some metadata I presume. The solution was easy: open a random port on the router and forward the traffic to the Icecast server: success! One caveat though: the LinkStation would be constantly streaming music, even when nobody was listening, causing non-stop churning of the harddisk. I wanted to started Ices when the first listener connected and to stop it once the last listener went away. That could be done by adding an authentication section to icecast.xml: <authentication type="url"> <option name="listener_add" value="http://localhost:8081/icecast.php"/> <option name="listener_remove" value="http://localhost:8081/icecast.php"/> <option name="auth_header" value="icecast-auth-user: 1"/> </authentication>With these settings, the Icecast server would contact the mentioned URLs when a listener (dis)connected. In the PHP file I could then start/stop the Ices server: <?
$action = $_POST['action'];
$mount = $_POST['mount'];
header("icecast-auth-user: 1");
function store($c)
{
$file = fopen("/tmp/count", "c+");
$count = fgets($file);
if ($count !== false)
rewind($file);
else
$count = 0;
$count = intval($count) + intval($c);
fputs($file, $count."\n");
fclose($file);
return $count;
}
function start_ices()
{
$array = array();
exec("ps", $array);
foreach($array as $line)
if (strpos($line, "/opt/bin/ices") !== false)
return;
exec("/opt/bin/ices -c /opt/etc/ices.conf.dist -B");
sleep(1);//echo $i;
}
function stop_ices()
{
exec("kill `cat /opt/var/log/icecast/ices.pid`");
}
if ($action == "listener_add" && $mount == "strm" && store(1) == 1)
start_ices();
if ($action == "listener_remove" && $mount == "strm" && store(-1) == 0)
stop_ices();
?>Success.
I can't believe how hard it is to create a global setting in PHP. I needed an atomic increment/decrement for the user count. The store($) function basically serves as a global InterlockedAdd, loading a integer from a tempfile, adding a value to it, writing the result back to the file and returning it to the caller. Please let me know if there's a easier way to accomplice that.
What's your mean?Friday, January 28. 2011
老外苦学汉语十年,到中国参加汉语等级考试,试题如下:
请解释下文中每个“意思”的意思— 阿呆给领导送红包时,两人的对话颇有意思。 领导:“你这是什么意思?” 阿呆:“没什么意思,意思意思。” 领导:“你这就不够意思了。” 阿呆:“小意思,小意思。” 领导:“你这人真有意思。” 阿呆:“其实也没有别的意思。” 领导:“那我就不好意思了。” 阿呆:“是我不好意思。” 老外泪流满面,交白卷回国了。 Chinese to English translation Foreigners learn Chinese bitter years, to China to participate in Chinese language level test, questions are as follows: Please explain below each "meaning" means — Dumb bribe the leaders, the two men rather meaningful dialogue. Leadership: "What do you mean? " Dumb: "no meaning, meaning mean. " Leadership: "You mean this is not enough. " Dumb: "a mere trifle, a mere trifle. " Leadership: "You are so interesting. " Dumb: "In fact, no other meaning. " Leadership: "Then I was embarrassed. "** Dumb: "I am sorry. " Foreigners in tears, has done nothing back home. ** "不好意思" means "I'm embarrassed", but it's also said when accepting a gift. Lǎowài kǔxué hànyǔ shí nián, dào zhōngguó cānjiā hànyǔ děngjí kǎoshì, shìtí rúxià: Qǐng jiěshì xià wénzhōng měi gè “yìsi” de yìsi— Ādāi gěi lǐngdǎo sòng hóngbāo shí, liǎng rén de duìhuà pō yǒuyìsi. Lǐngdǎo:“Nǐ zhè shì shénme yìsi?” Ādāi:“Méishénme yìsi, yìsi yìsi.” Lǐngdǎo:“Nǐ zhè jiù bùgòu yìsi le.” Ādāi:“Xiǎoyìsi, xiǎoyìsi.” Lǐngdǎo:“Nǐ zhè rén zhēn yǒuyìsi.” Ādāi:“Qíshí yě méiyǒu bié de yìsi.” Lǐngdǎo:“Nà wǒ jiù bù hǎoyìsi le.” Ādāi:“Shì wǒ bù hǎoyìsi.” Lǎowài lèi liú mǎnmiàn, jiāobáijuàn huíguó le. "110V" really means 110VMonday, August 16. 2010
Long story short: I've been to the US a few weeks ago and I thought I'd pick up a new Xbox 360 250GB while I was there, seeing as those are hard to find in China. I assumed its power supply would accept both 110V and 220V (like my PS3 and most modern PCs and laptops.) Well, it did not.
Of course, I did not just plug it in and watch explode. I first opened up the power supply (not an easy feat, let me tell you) and only plugged it in after noticing the board inside had "250V" written on it, with a check mark next to it. Not sure what that meant though. It still blew up after I plugged it in. I already had a plan B from the moment I bought the Xbox: the power supply output 12V and 5V, much like a regular PC power supply. So I bought a (mini) ATX power supply and went online to see if I could find the pinout. Plenty. When I plugged in the ATX power source, I measured the voltage on a few pins, but got nothing. Then I remembered the fact that an ATX power supply does not really turn on until the PC tells it to. This is done by shorting pin 14 of the ATX plug (green wire) to ground (black, any will do.) Once I did that, I got all the power I needed. The pinout on the Xbox side was harder to come by. I had found a few for the old Xbox 360, but none for the new slim model. Fortunately there were some hints on the PSU's board: Yellow 12V I thought I'd check what I could get away with: I connected the yellow wire from the Xbox to the yellow wire of the PSU. Same for the red wires, and black wires, matching the colors. Then, I turned on the power supply by connecting the green wire on pin 14 to a black wire. I pressed the button on the Xbox: success! Having the power supply turned on all the time was not something I liked though. So the next step was to get the power supply to turn on together with the Xbox. By measuring the voltage I saw the blue PS_ON wire being pulled up (~3.3V) when the Xbox wants to turn on. In order to do this, the Xbox needs some power though. This is done by the red "5VSB" wire. The "SB" stands for Stand By. This means that the red wire from the Xbox must not be attached to the red ATX wires, but to the purple wire on pin 9. This page explained how: use a transistor or optocoupler that will connect the green ATX wire to GND when the blue Xbox wire goes up. I had no optocoupler lying around, or so I thought. I then noticed the blown up power supply on the table. There were some things on it that looked a lot like optocouplers. Sure enough: The datasheet for the EL817 can be found here. So, pin 1 of the optocoupler (the pin near the dot) will be pulled high, to ~3.3V. We want to give the LED inside about ~1.2V, that leaves 2.1V for a resistor. At about 20mA this means we need a V/I=R 2.1/0.02= ~100 Ohm resistor. 100 Ohm = 10*10, 10 times 10 to the power 1: we need a resistor with Brown, Black, Brown rings. Luckily I found one I had everything I needed: Yellow (Xbox) - Yellow (ATX) I had no idea what the gray RSENSE wire was for, so I just ignored it and it worked fine! Once I got everything figured out, I opened up the ATX PSU and removed all the wires I didn't use. Important: there was a brown wire that got connected to a orange (3.3V) wire IN the ATX power connector. So after cutting of the connector I had to connect the brown wire to 3.3V somewhere inside the PSU. And all it cost me was an ATX power supply and 1 blister. Not bad UPDATE: Here's the schematic for the whole thing:
Free memoryWednesday, July 14. 2010
My VPS was running out of memory so I started googling to see what could be done about it. The best tip was this:
SSH into the system with putty or some other ssh utility My my.cnf happened to be in /etc/mysql/my.cnf, but after restart mysqld was using 80MB less memory! Before you do this though, make sure you don't have any tables using innodb or bdb. Well, that took long enough...Wednesday, June 23. 2010
New in Windows 7: select some files in explorer and press SHIFT + RIGHT-CLICK on the selection. Lo and behold, a secret option appears: "Copy As Paths"!
Similarly, SHIFT + RIGHT-CLICK on a folder offers "Open command window here" and "Open in new process." The things you learn when you work at MS. China Mobile SIMs explainedSaturday, May 15. 2010
A reader (!) complained about not being able to get GPRS to work with China Mobile. Although I'm in no way an expert on the subject, I think I know enough to make a useful remark, or two..
The problem with China Mobile is that they have two kinds of SIM cards: 神州行 ShenZhouXing, also known as EasyOwn, versus 动感地带 DongGanDiDai also known as M-Zone.
M-Zone supports GPRS and EDGE. Even 3G, but China Mobile's 3G standard is not UMTS or HSDPA, but some other TD-SCDMA standard, which is not supported by most phones that originate from abroad. For what it's worth, I thought EDGE worked quite well, to be honest. When using 3G, China's internet connection is probably the limiting factor. There are two ways to tell which card you have. First of all, the actual SIM cards are of a different color. ShenZhouXing SIMs are green, whereas M-Zone are orange. Also, when you turn on your phone (at least with Android) it should popup a message saying what SIM you have. For example, my phone shows something like 没错!我是动感地带!"Not Bad! I'm M-Zone!" Apart from being able to use GPRS, receiving calls with M-Zone is FREE, contrary to ShenZhouXing where you pay when you pick up the phone. Note that M-Zone does require you to subscribe to some kind of package deal, which means some money will be subtracted at the beginning of each month. The cheapest package is about 11 RMB, if I remember correctly, and includes local (Beijing only) minutes, short messages and 20MB of traffic. If you currently have a ShenZhouXing SIM, you can change it into an M-Zone SIM by going to your local China Mobile office (real CM office, not just any reseller.) Bring your passport and the PUK code of the ShenZhouXing SIM. The PUK code is written on the bank-card-shaped piece of plastic that used to contain the SIM card. The change will active from the beginning of the next month. Hope this helps. New phone numberTuesday, May 11. 2010
A few days ago I've moved to Shanghai and, since using a Beijing phone number in Shanghai is expensive, I decided to get a new phone number. I've decided to go with China Unicom, because their 3G standard is the same as the rest of the world's, so now I finally have 3G! (Although, youtube is still blocked, but at least Android 2.1 supports VPN now.)
To get my new Shanghai phone number, add 3410941632 to my old phone number Converting mailbox format from mbox to MaildirThursday, April 29. 2010
The default mailbox format used by postfix and dovecot is the mbox format. This basically boils down to a single file per user in /var/mail/ with all mails concatenated, one after the other. (Mail in folders other than inbox are stored in ~/mail/)
Mail stored in the Maildir format uses 1 file per mail. Accessing individual mails is faster, but because there are many small files, the disk space overhead is bigger. The mbox format is fairly simple and compact, but has a couple of problems. For one, being a single flat file it's easy to append new incoming mails, but it's not straightforward to remove a mail from the middle of the file. Another problem that I've noticed in Thunderbird 3 is that sometimes two mails would appear to be combined. I'd suddenly see another mail's attachments appearing on a mail without attachments. I'm not sure whether this is a problem with Thunderbird, dovecot, or perhaps a corrupt mbox file? The steps:
You should also stop postifx to prevent mail being delivered to the wrong mailbox during conversion: sudo /etc/init.d/postfix stop The easiest way to convert the mailbox format is to let dovecot do it for us. Make the following changes to /etc/dovecot/dovecot.conf: mail_location = maildir:~/Maildir(Search for the settings and change them.) Apply the changes by restarting dovecot: sudo /etc/init.d/dovecot restart Now start your mail client. Dovecot will convert the mailbox at login, so this might take a while. Also, chances are the client will start to download all mail again. (There seem to be some scripts that can prevent this.) Before we start postfix, edit /etc/postfix/main.cf to tell it where to deliver the mail: home_mailbox = Maildir/ Restart postfix: sudo /etc/init.d/postfix start
(Page 1 of 8, totaling 120 entries)
» next page
Competition entry by David Cummins powered by Serendipity v1.0 |
QuicksearchMy Favorites
CategoriesTimezones
Blog Administration |