Last active
August 29, 2015 14:15
-
-
Save dmpop/d26c08e0b9fe5612c292 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Unofficial Shoot and View Receiver for Transcend Wi-Fi SD card | |
# Original version: http://sourceforge.net/projects/transsvr/ | |
# This script was developed by Sven Angelmahr | |
# Contact [email protected] for further details. | |
# DEPENDENCIES | |
# The script require Perl and wget | |
# DESCRIPTION | |
# This script searches the local network for a Transcend Wi-Fi SD Card. | |
# When the card is found, the script enables the Shoot & View mode, and automatically | |
# downloads new photos as soon as it detects them using the wget tool. The script | |
# a pause (~2 sec.) between photos to work properly. | |
# The script transfers JPEG files only. | |
# CONFIGURATION | |
# If your machine is connected to the card's wireless network, and you haven't changed | |
# default user name and password, then no additional configuration is necessary. | |
# Otherwise, you need to specify the correct values for the $BroadcastIP, | |
# $User and $Password parameters. | |
use strict; | |
use warnings; | |
use IO::Socket::INET; | |
use Net::Ping; | |
# ==== Config by User! ==== | |
my $BroadcastIP = "192.168.11.255"; | |
my $User="Admin"; | |
my $Password="Admin"; | |
# ==== End Config by User / No Changes behind this line ==== | |
my $CardIP = "0.0.0.0"; | |
START: | |
#Check if card is alive and if so go to Shoot&View mode | |
print "Searching card...\n"; | |
&GoFindCard; | |
#print "\nFound Card: ",$CardIP,"\n"; | |
if (&KeepCardAlive eq "OK") { | |
print "\nEnabling Shoot&View Mode\n"; | |
my $ShootAndView = IO::Socket::INET->new(Proto=>"tcp",PeerPort=>5566,PeerAddr=>$CardIP) | |
or die "Can't make TCP socket: $@"; | |
my ($datagram1,$flags1); | |
until(1!=1) { | |
eval { | |
local $SIG{ALRM} = sub { die "alarm\n" }; | |
alarm 5; # Set Timer to check if card is still alive every XX seconds by TCP ECHO (not ICMP!) | |
$ShootAndView->recv($datagram1,250,$flags1); | |
alarm 0; # if we get a info about new files we disable the alarm here | |
}; | |
if ($@) { | |
die unless $@ eq "alarm\n"; #if we got the alarmtime we have to check if card is alive | |
#if card is unreachable we have to find it again! | |
if (&KeepCardAlive eq "NOK") { | |
print "\nClosing Shoot And View...\n"; | |
close($ShootAndView); | |
print "Back to start...\n"; | |
goto START; | |
}; | |
} | |
else { | |
print "\nThere's something new on ", $ShootAndView->peerhost,": $datagram1\n"; | |
#And now for some regex! --> Remove >/mnt from >/mnt/sd/DCIM/200MSDCF/_DSC3233.JPG | |
$datagram1 =~ s/^>\/mnt//; | |
#system('wget http://admin:[email protected]/sd/DCIM/200MSDCF/_DSC3233.JPG'); | |
my $wget = join("","wget -bqc http://",$User,":",$Password,"@",$CardIP,$datagram1,); | |
system($wget); | |
}; | |
}; | |
} | |
else { | |
print "\nNothing found. Waiting 10 Seconds...\n"; | |
sleep(10); | |
goto START; | |
}; | |
sub KeepCardAlive { | |
my $RC; | |
#Keep Card Alive! | |
#Client sendet TCP an 192.168.11.254; Source Port variabel; Destination Port 7; Flag Set SYN | |
#Karte sendet TCP an 192.168.11.12; Source Port 7; Destination Port variabel; Flag Set ACK; RST | |
my $p=Net::Ping->new('tcp'); | |
if ($p->ping($CardIP, 5)==1) { | |
$RC = "OK"; | |
} | |
else { | |
$RC = "NOK"; | |
}; | |
return($RC); | |
}; | |
sub GoFindCard { | |
#Find Transcend SD WiFi Card | |
#Sending UDP stolen by: http://www.perlmonks.org/?node_id=129521 | |
STARTFIND: | |
eval { | |
local $SIG{ALRM} = sub { die "alarm\n" }; | |
alarm 5; # Set Timer to wait for card to answer, aboard and retry if card doesn't answer. | |
#Open Port for Broadcast | |
my $FindCard = IO::Socket::INET->new(Proto=>"udp",LocalPort=>58255,PeerPort=>55777,PeerAddr=>$BroadcastIP,Broadcast=>1) | |
or die "Can't make UDP socket for Broadcast. Reason: $@"; | |
#Send some nonsense, original AndroidAPP also does this | |
$FindCard->send(""); | |
#Close Broadcastport | |
close($FindCard); | |
#Open Port for receiving Unicast from card (hopefully!) | |
my $recvCard = IO::Socket::INET->new(Proto=>"udp",LocalPort=>58255) | |
or die "Can't make UDP socket for catching Unicast. Reason: $@"; | |
my ($datagram,$flags); | |
$recvCard->recv($datagram,200,$flags); #Datagramm may be longer than 200 chars! (depending on SSID!) | |
if (index($datagram,"Transcend WiFiSD")>=0) { | |
#print "Found Card!\n", $FindCard->peerhost,", flags ",$flags || "none",": $datagram\n"; | |
$CardIP=$recvCard->peerhost; | |
}; | |
close($recvCard); | |
alarm 0; | |
}; | |
if ($@ eq "alarm\n") { | |
print "No Transcend Card Found, trying again...\n"; | |
goto STARTFIND; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment