Episode 4x24, PHP Twitter Tamagotchi and ROFLcon |
![]() ![]() |
Episode 4x24, PHP Twitter Tamagotchi and ROFLcon |
Wed, 28 Jan 2009 13:07:01 +0000
Post
#1
|
|
![]() Hak.5 Junkie ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 3,120 Joined: Tue, 26 Jul 2005 15:52:42 +0000 From: Williamsburg, VA Member No.: 2 |
Hot off the heels of ROFLcon in NYC the crew kicks some in studio technolust. Darren shares with you his custom twitter tamagotchi bot written in PHP with some sweet RSS parsing, VLC controlling, curl loving nested goodness. Plus guest Jim Louderback, gadget lover and Revision3 stage hand, joins us to talk about all things new media. Download CODE <?php
require_once('magpierss/rss_fetch.inc'); $previous_items = "1"; //There can only be one define('MAGPIE_CACHE_ON', 0); //Tell Magpie not to cache define('MAGPIE_CACHE_AGE', 0); //Mo cache mo problems $loopcount=1; while ($title != "#roflbot *exit*") { //kill command $url = "http://search.twitter.com/search.atom?q=roflbot"; //what are we looking for $num_items = 1; $rss = fetch_rss($url); $items = array_slice($rss->items, 0, $num_items); echo "..."; // Open last logfile as array so we can check for duplicates //$previous_items = file('rsslogs.txt'); $current_items = array(); foreach ($items as $item) { $href = $item['link']; $title = $item['title']; //Really we just care about title but meh $desc = $item['description']; //just incase we define link and description $item_checksum = $title; // Use an MD5 hash of the link as unique identifier // Actually we're not using an MD5 now and I forget why //echo "..debug: " . $title . $desc . ".."; if ($loopcount >= 30) { //no activity for 5 minutes echo "I've been idle too long. Doing something new. "; $bored = rand(1,10); echo "Randomly picked " . $bored . "\n"; //This is so nasty its not even funny. I had previously done a switch(rnd(1,10)) with cases to set $title but it failed and I ran out of time. Nasty nasty hack job. if ($bored == 1) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=6 -G > output.txt"); echo "\nCry"; $loopcount=0; } if ($bored == 2) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=7 -G > output.txt"); echo "\nSleep"; $loopcount=0; } if ($bored == 3) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=14 -G > output.txt"); echo "\nExplode"; $loopcount=0; } if ($bored == 4) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=10 -G > output.txt"); echo "\nDance"; $loopcount=0; } if ($bored == 5) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=9 -G > output.txt"); echo "\nLove"; $loopcount=0; } if ($bored == 6) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=8 -G > output.txt"); echo "\nEat"; $loopcount=0; } if ($bored == 7) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=11 -G > output.txt"); echo "\nFight"; $loopcount=0; } if ($bored == 8) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=12 -G > output.txt"); echo "\nFail"; $loopcount=0; } if ($bored == 9) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=13 -G > output.txt"); echo "\nDrink"; $loopcount=0; } if ($bored == 10) { system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=5 -G > output.txt"); echo "\nBlink"; $loopcount=0; } } if ($item_checksum != $previous_items) //if this is a new command { //echo "\nNew command: " . $title; //turn off when not debugging //echo "\n\nTitle=" . $title; switch ($title) { //process commands case "roflbot dance": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=10 -G > output.txt"); echo "\nDance"; $loopcount=0; break; case "roflbot love": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=9 -G > output.txt"); echo "\nLove"; $loopcount=0; break; case "roflbot feed": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=8 -G > output.txt"); echo "\nFeed"; $loopcount=0; break; case "roflbot eat": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=8 -G > output.txt"); echo "\nEat"; $loopcount=0; break; case "roflbot sleep": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=7 -G > output.txt"); echo "\nSleep"; $loopcount=0; break; case "roflbot cry": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=6 -G > output.txt"); echo "\nCry"; $loopcount=0; break; case "roflbot fight": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=11 -G > output.txt"); echo "\nFight"; $loopcount=0; break; case "roflbot attack": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=11 -G > output.txt"); echo "\nAttack"; $loopcount=0; break; case "roflbot fail": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=12 -G > output.txt"); echo "\nFail"; $loopcount=0; break; case "roflbot tweet": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=12 -G > output.txt"); echo "\nTweet"; $loopcount=0; break; case "roflbot drink": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=13 -G > output.txt"); echo "\nDrink"; $loopcount=0; break; case "roflbot drunk": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=13 -G > output.txt"); echo "\nDrunk"; $loopcount=0; break; case "roflbot party": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=13 -G > output.txt"); echo "\nParty"; $loopcount=0; break; case "roflbot explode": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=14 -G > output.txt"); echo "\nExplode"; $loopcount=0; break; case "roflbot die": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=14 -G > output.txt"); echo "\nDie"; $loopcount=0; break; case "roflbot gtfo": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=14 -G > output.txt"); echo "\nGTFO"; $loopcount=0; break; case "roflbot blink": system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=5 -G > output.txt"); echo "\nBlink"; $loopcount=0; break; } //end case } else { /*echo "\nWaiting for new command";*/ } echo " " . $loopcount; // Add this item to the new log //$current_items[] = $item_checksum."\n"; $previous_items = $item_checksum; } // end for each $items as $item sleep ("10"); $loopcount=$loopcount+1; } // end while system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=shutdown -G > output.txt"); ?> -------------------- |
|
|
|
Wed, 28 Jan 2009 13:09:10 +0000
Post
#2
|
|
|
Hak.5 Fan ![]() ![]() Group: Members Posts: 16 Joined: Wed, 28 Jan 2009 12:46:21 +0000 Member No.: 12,707 |
Just a quick one:
I don't like to complain and correct people but assuming it isn't due to a cultural difference I'm pretty sure Magpie RSS is pronounced mag-PIE (like the bird - the bird they actually show on their sourceforge page) rather than mag-pee. Just think of this, you eat pie not pee Otherwise good so far, still watching it P.S. any chance of the HD RSS feed getting onto hak5.org (rather than just revision3)? |
|
|
|
Wed, 28 Jan 2009 14:46:31 +0000
Post
#3
|
|
![]() Hak.5 Junkie ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 3,120 Joined: Tue, 26 Jul 2005 15:52:42 +0000 From: Williamsburg, VA Member No.: 2 |
HD RSS Feed will be posted later today. I'm doing all sorts of website updates so if you can think of anything else I need to fix let me know soon.
As for the Magpie thing, go ahead and add that to the list of shit Darren can't pronounce. Like Yahsager and "C.O dot U.K" -------------------- |
|
|
|
Wed, 28 Jan 2009 15:00:31 +0000
Post
#4
|
|
![]() Hak.5 1337 Fan ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Active Members Posts: 1,550 Joined: Fri, 26 Jan 2007 17:21:30 +0000 From: Dundee, Scotland Member No.: 5,669 |
Very cool, makes me wish I still had my tamagotchi.
One addition you could make, is let it have its own twitter channel, and if it hasnt been fed in a while, it will tweet something like "feed me" etc. Just a Thought. -------------------- Oh what a lovely picture you have drawn, I saved it to a special file called /dev/null
English to Na'vi (AVATAR) Translator |
|
|
|
Wed, 28 Jan 2009 15:11:22 +0000
Post
#5
|
|
![]() Hak.5 Junkie ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 3,120 Joined: Tue, 26 Jul 2005 15:52:42 +0000 From: Williamsburg, VA Member No.: 2 |
Very cool, makes me wish I still had my tamagotchi. One addition you could make, is let it have its own twitter channel, and if it hasnt been fed in a while, it will tweet something like "feed me" etc. Just a Thought. Thats great. I bet I could easily do that with the code snippet over at http://www.modaco.com/content/the-lounge/2...tatus-from-php/ -------------------- |
|
|
|
Wed, 28 Jan 2009 15:49:57 +0000
Post
#6
|
|
![]() Newbie Group: Members Posts: 7 Joined: Wed, 26 Nov 2008 23:00:08 +0000 From: Seattle, WA Member No.: 11,858 |
Twit-agochi! haha LOVE IT!
Reminded me of the Digimon battle pets from back in the day. Be interesting to see if anyone can formulate a similiar system you designed for your twit-agochi but with a battling sub-system. If no one feels inspired by that, I think I'll add this to my ever growing list of projects! Very neat idea Darren! |
|
|
|
Wed, 28 Jan 2009 17:56:24 +0000
Post
#7
|
|
![]() Hak.5 Junkie ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 3,120 Joined: Tue, 26 Jul 2005 15:52:42 +0000 From: Williamsburg, VA Member No.: 2 |
Twit-agochi! haha LOVE IT! Reminded me of the Digimon battle pets from back in the day. Be interesting to see if anyone can formulate a similiar system you designed for your twit-agochi but with a battling sub-system. If no one feels inspired by that, I think I'll add this to my ever growing list of projects! Very neat idea Darren! I wanted to add a function so that you could send a command to the roflbot with two twitter @usernames and have them battle but with VLC as the display mechanism I can't really do anything dynamic. -------------------- |
|
|
|
Wed, 28 Jan 2009 18:44:26 +0000
Post
#8
|
|
|
Newbie Group: Members Posts: 1 Joined: Wed, 28 Jan 2009 18:41:07 +0000 Member No.: 12,713 |
At the end of the show you had an idea for updating the IP address of multiple windows machines using a batch file using at to schedule it. You should be able to generate a list of IPs and a list of hostnames and simply have the script look up the hostname of the machine it's being run on and then find the right IP in the list. Shouldn't even be all that hard! Should only take 1 loop per octet to generate the list.
|
|
|
|
Wed, 28 Jan 2009 23:52:22 +0000
Post
#9
|
|
![]() Hak.5 Fan ![]() ![]() Group: Members Posts: 19 Joined: Sun, 05 Oct 2008 11:07:45 +0000 Member No.: 11,082 |
At the end of the show you had an idea for updating the IP address of multiple windows machines using a batch file using at to schedule it. You should be able to generate a list of IPs and a list of hostnames and simply have the script look up the hostname of the machine it's being run on and then find the right IP in the list. Shouldn't even be all that hard! Should only take 1 loop per octet to generate the list. What about sending out a batch file to all the computers that you need to update, just have it retrieve the ip from a second batch file on the network. The batch file on the network could keep track of the ips it has given out via a text file. The only problem i see is if all the computers try and run the batch file at the same time, is there a way you could stagger when the computers ran the file? -------------------- ![]() |
|
|
|
Thu, 29 Jan 2009 06:46:03 +0000
Post
#10
|
|
|
Newbie Group: Members Posts: 1 Joined: Thu, 29 Jan 2009 06:38:13 +0000 Member No.: 12,720 |
Great episode as usual.
Glad to hear your first distro was Slackware, Darren! Being fairly young (20) I started in the CD (not DVD though!) days not the floppies (PS. TY for the signed photo with the stickers, already framed it |
|
|
|
Thu, 29 Jan 2009 09:08:29 +0000
Post
#11
|
|
|
Hak.5 Zombie ![]() ![]() ![]() ![]() ![]() Group: Active Members Posts: 153 Joined: Wed, 09 Jul 2008 13:09:44 +0000 From: Cyberspace, or was it Sweden Member No.: 10,231 |
Darren, you do know there is a CURL extension for PHP
http://se2.php.net/CURL |
|
|
|
Thu, 29 Jan 2009 10:37:37 +0000
Post
#12
|
|
![]() Hak.5 Pirate ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 336 Joined: Sat, 03 Jan 2009 01:54:20 +0000 From: Minneapolis, MN Member No.: 12,442 |
Ahh, it was enlightening to see it roll back to the traditional format.
I hope you can have your Kitchen segments sprinkled in every once in awhile, I really liked the 2009 style of these first few episodes. -------------------- |
|
|
|
Thu, 29 Jan 2009 11:16:01 +0000
Post
#13
|
|
|
Hak.5 Fan ![]() ![]() Group: Members Posts: 16 Joined: Wed, 28 Jan 2009 12:46:21 +0000 Member No.: 12,707 |
What about sending out a batch file to all the computers that you need to update, just have it retrieve the ip from a second batch file on the network. The batch file on the network could keep track of the ips it has given out via a text file. The only problem i see is if all the computers try and run the batch file at the same time, is there a way you could stagger when the computers ran the file? This race condition is commonly overcome with a lock file or a semaphore of some sort. If something is already asking for an IP address, wait until it is done and then try again (usually a small but random wait). If that isn't possible in batch, it may be better to use vbs or even C, C#, python, dare I say php, etc (depending on what is available). Alternatively, they could just be scheduled so that they are staggered. If you know the order they ask for them in (and are sure that none will be skipped because they are turned off or whatever), you can infer which IP address they should get. I would have thought the bigger problem with this approach would be keeping some kind of structure for the topology (e.g. one range of IP addresses for one physical room). If the host name is already structured, that could be used to structure the new IP addresses too without having to build a table for it. As for the Magpie thing, go ahead and add that to the list of shit Darren can't pronounce. Like Yahsager and "C.O dot U.K" I'm glad you took it well. I didn't mean any offence but getting your pronunciation corrected is never nice. Just goes to show the awesomeness of Darren! |
|
|
|
Thu, 29 Jan 2009 13:08:55 +0000
Post
#14
|
|
![]() Hak.5 Junkie ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 3,120 Joined: Tue, 26 Jul 2005 15:52:42 +0000 From: Williamsburg, VA Member No.: 2 |
Darren, you do know there is a CURL extension for PHP http://se2.php.net/CURL No, actually I didn't know that. Had I known I wouldn't have used a system call to curl. Still, the principals of the segment are somewhat sound which is amazing coming from this rusty ass script jockey. This race condition is commonly overcome with a lock file or a semaphore of some sort. If something is already asking for an IP address, wait until it is done and then try again (usually a small but random wait). If that isn't possible in batch, it may be better to use vbs or even C, C#, python, dare I say php, etc (depending on what is available). Alternatively, they could just be scheduled so that they are staggered. If you know the order they ask for them in (and are sure that none will be skipped because they are turned off or whatever), you can infer which IP address they should get. I would have thought the bigger problem with this approach would be keeping some kind of structure for the topology (e.g. one range of IP addresses for one physical room). If the host name is already structured, that could be used to structure the new IP addresses too without having to build a table for it. I'm glad you took it well. I didn't mean any offence but getting your pronunciation corrected is never nice. Just goes to show the awesomeness of Darren! I'm thinking about writing a script that'll simply take the last octet of the current IP and use that as the last octet of the new IP. Would be like 4 lines in bash but the viewer wrote in about windows which makes it a game of "how do i trick you into doing wtf I want". Maybe this would be a good reason to show off that php compiler I've been meaning to tout on the show. ...Hey, when all you have is a hammer -------------------- |
|
|
|
Thu, 29 Jan 2009 13:41:04 +0000
Post
#15
|
|
![]() Wait, What? ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 160 Joined: Fri, 09 May 2008 17:28:07 +0000 From: Williamsburg, VA Member No.: 9,885 |
No, actually I didn't know that. Had I known I wouldn't have used a system call to curl. Still, the principals of the segment are somewhat sound which is amazing coming from this rusty ass script jockey. I'm thinking about writing a script that'll simply take the last octet of the current IP and use that as the last octet of the new IP. Would be like 4 lines in bash but the viewer wrote in about windows which makes it a game of "how do i trick you into doing wtf I want". Maybe this would be a good reason to show off that php compiler I've been meaning to tout on the show. ...Hey, when all you have is a hammer The easiest way I could think darren is a .vbs script. I've seen some crazy ass vbs scripting in my day, can't imagine that wouldn't be possible. Matt -------------------- Former co-host of Hak5
http://www.mattlestock.com |
|
|
|
Thu, 29 Jan 2009 23:45:05 +0000
Post
#16
|
|
![]() Newbie Group: Members Posts: 1 Joined: Thu, 29 Jan 2009 23:32:49 +0000 From: NY Member No.: 12,735 |
Not to tear you up to bad but that was some horribly bad kludgery. For a little cleanup likes 30-85 can be replaced with this much simpler version.
CODE if ($loopcount >= 30) { //no activity for 5 minutes echo "I've been idle too long. Doing something new. "; $choice = rand(0,9); echo "Randomly picked " . ( $choice + 1 ) . "\n"; $bored = array(6,7,14,10,9,8,11,12,13,5); $choice = $bored[$choice]; system('curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=' . $choice . ' -G > output.txt'); $loopcount = 0; } 101-186, also horrible kludgery. Arrays are your friend CODE $actions = array("dance"=>10,"love"=>9,"feed"=>8,"eat"=>8,"sleep"=>7, "cry"=>6,"fight"=>11,"attack"=>11,"fail"=>12,"tweet"=>12, "drink"=>13,"drunk"=>13,"party"=>13,"explode"=>14, "die"=>14,"gtfo"=>14, "blink"=>5); $fix_title = preg_replace('/^roflbot /', '', strtolower($title)); system('curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=' . $actions[$fix_title] . ' -G > output.txt'); $loopcount=0; So, all in all, cutting your code by about half. This was tossed together in haste after my eyes started bleeding looking at the code and I didn't test it so feel free to flame me if I missed a semicolon somewhere. |
|
|
|
Fri, 30 Jan 2009 08:37:21 +0000
Post
#17
|
|
![]() Wait, What? ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 160 Joined: Fri, 09 May 2008 17:28:07 +0000 From: Williamsburg, VA Member No.: 9,885 |
shawncplus,
Darren did give a warning before he even started the segment that he new that his code was neither optimized, nor pretty. We're not programmers by trade, but we can hack together a working model, and it's interesting to see how other people take the idea and either clean it up or enhance it. Thanks for watching, and your right, that is a whole lot cleaner Matt -------------------- Former co-host of Hak5
http://www.mattlestock.com |
|
|
|
Fri, 30 Jan 2009 10:11:04 +0000
Post
#18
|
|
![]() Wait, What? ![]() ![]() ![]() ![]() ![]() Group: Root Admin Posts: 160 Joined: Fri, 09 May 2008 17:28:07 +0000 From: Williamsburg, VA Member No.: 9,885 |
The HD RSS feeds have been posted on the main page.
Thanks for the heads up guys. Matt -------------------- Former co-host of Hak5
http://www.mattlestock.com |
|
|
|
Fri, 30 Jan 2009 23:25:45 +0000
Post
#19
|
|
![]() I finally found out how to edit this and I have nothing to put : ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Active Members Posts: 591 Joined: Thu, 19 Jun 2008 22:07:35 +0000 Member No.: 10,129 |
Warning
################################# ****************************************** ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the command line change ip address gave me some problems (I couldm't connect to the internet) You just have to go to the connection in My Network Connection right click it select Properties and then click Internet Protocol on the General Tab and click obtain ip address automatically and then click obtain DNS Server Address Automatically Python iP address thingy Just Create a file with COMPUTERNAME IPADRESS i.e BOB 192.168.1.2 STEVE 192.168.1.3 Written in Python CODE CODE import os, subprocess
x=os.getenv('COMPUTERNAME') ip='f' lines = [line.rstrip('\n') for line in file(r'C:\Documents and Settings\David\Desktop\IPHAK5,txt')] for n, i in enumerate(lines): if ip=='t': ipadress.append(lines[n]) ip='f' continue if ip=='f': computer.append(lines[n]) ip='t' continue for n, i in enumerate(computer): if i == x: global var var=nipadressvar=ipadress[var] ipadressvar=ipadress[var] subprocess.Popen('netsh int ip set address "local area connection" static '+ipadressvar+' 255.255.255.0 192.168.0.254 1') |
|
|
|
Sat, 31 Jan 2009 08:27:06 +0000
Post
#20
|
|
|
Hak.5 Fan ![]() ![]() Group: Members Posts: 16 Joined: Wed, 28 Jan 2009 12:46:21 +0000 Member No.: 12,707 |
If you are going to do that (assuming python is the hammer available, etc) you may as well just use a dictionary inside the actual python file, making the python file pretty much 1 dictionary of hosts (or old IP addresses) to new ones and then 1 line of code to run the netsh command line, passing in the current data as the key to the dictionary and concatenating that into the command line to run.
I expect it is also possible entirely inside the Windows PowerShell if that is available but I can't get my head around that massive list of weird, long named, hyphenated commands. If you didn't have internet access working after the command line change, your network is probably set up differently to the example (e.g. common default gateways for home LANs are typically one of 192.168.0.1, 192.168.1.1 or 192.168.0.254). In this case, they are moving from 192.168.*.* to something like 10.*.*.* so the default gateway would change anyway and someone in control of such a large network is unlikely to make such mistakes. By the way, Python supports booleans so you don't have to use 't' and 'f' strings (you could use the True and False keywords) and if you are going to use a boolean to toggle between the lines like that, you may as well just read 2 lines on each pass of the loop. |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: Thu, 29 Jul 2010 16:34:42 +0000 |