2
0
mirror of https://github.com/narkoz/hacker-scripts synced 2025-08-23 10:57:13 +00:00

Merge pull request #52 from robaganrab/perl-implementation

Add Perl Implementation
This commit is contained in:
Nihad Abbasov 2015-11-27 22:31:55 +04:00
commit 8853685241
4 changed files with 282 additions and 0 deletions

49
perl/fucking-coffee.pl Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use YAML;
use Net::Telnet;
# Config
my $conf = Load( <<'...' );
---
coffee_machine_ip: 10.10.42.42
password: 1234
password_prompt: Password:
delay_before_brew: 17
delay: 24
...
# Skip on weekends
my $date = DateTime->now;
if ( $date->day_of_week >= 6 ) {
exit;
}
# Exit early if no sessions with my username are found
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
my @sessions = grep {
m/$ENV{'USER'}/
} <$cmd_who>;
close $cmd_who;
exit if ( scalar( @sessions ) == 0 );
sleep $conf->{'delay_before_brew'};
my $con = Net::Telnet->new(
'Host' => $conf->{'coffee_machine_ip'},
);
$con->watifor( $conf->{'password_prompt'} );
$con->cmd( $conf->{'password'} );
$con->cmd( 'sys brew' );
sleep $conf->{'delay'};
$con->cmd( 'sys pour' );
$con->close;

73
perl/hangover.pl Executable file
View File

@ -0,0 +1,73 @@
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use SMS::Send;
use YAML;
# Config
my $conf = Load( <<'...' );
---
phone_numbers:
my_number: +15005550006
boss_number: +xxx
reasons:
- Locked out
- Pipes broke
- Food poisoning
- Not feeling well
...
my $date = DateTime->now;
# Skip on weekends
if ( $date->day_of_week >= 6 ) {
exit;
}
# Exit early if no sessions with my username are found
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
my @sessions = grep {
m/$ENV{'USER'}/
} <$cmd_who>;
close $cmd_who;
exit if ( scalar( @sessions ) == 0 );
# Load Twilio API config
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
LINE: while ( my $line = <$env> ) {
next LINE unless ( $line =~ m/^(TWILIO[^=]+)=(.*)(?:[\n\r]*)/ );
$conf->{'env'}->{ $1 } = $2;
}
close $env;
# Randomize excuse
my $reason_number = int( rand( scalar( @{ $conf->{'reasons'} } ) ) );
my $sms_text = "Gonna work from home. ". $conf->{'reasons'}[ $reason_number ];
# Create an object. There are three required values:
my $sender = SMS::Send->new('Twilio',
_accountsid => $conf->{'env'}->{'TWILIO_ACCOUNT_SID'},
_authtoken => $conf->{'env'}->{'TWILIO_AUTH_TOKEN'},
_from => $conf->{'phone_numbers'}->{'my_number'},
);
# Send a message to me
my $sent = $sender->send_sms(
text => $sms_text,
to => $conf->{'phone_numbers'}->{'boss_number'},
);
# Did it send?
if ( $sent ) {
print "Sent message.\n";
} else {
print "Message failed.\n";
}

88
perl/kumar-asshole.pl Executable file
View File

@ -0,0 +1,88 @@
#!/usr/bin/perl
use strict;
use warnings;
use YAML;
use DateTime;
use Mail::Webmail::Gmail;
# Config
my $conf = Load( <<'...' );
---
kumar_mail: kumar.a@examle.com
database_regex: \S+_staging
keywords_regex: sorry|help|wrong
backup_path: /home/backups/databases/
...
$conf->{'database_regex'} = qr/ ( $conf->{'database_regex'} ) /x;
$conf->{'keywords_regex'} = qr/ ( $conf->{'keywords_regex'} ) /x;
my $date = DateTime->now->subtract(
'days' => 1
);
# Load GMail API config
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
LINE: while ( my $line = <$env> ) {
next LINE unless ( $line =~ m/^(GMAIL[^=]+)=(.*)(?:[\n\r]*)/ );
$conf->{'env'}->{ $1 } = $2;
}
close $env;
my $gmail = Mail::Webmail::Gmail->new(
username => $conf->{'env'}->{'GMAIL_USERNAME'},
password => $conf->{'env'}->{'GMAIL_PASSWORD'},
encrypt_session => 1,
);
my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } );
die "Cannot fetch emails: ". $gmail->error_msg();
MESSAGE: foreach my $message ( @{ $messages } ) {
unless (
( $message->{ 'new' } )
&& ( $message->{'sender_email'} eq $conf->{'kumars_email'} )
&& ( $message->{'body'} =~ m/$conf->{'keywords_regex'}/ )
&& ( $message->{'body'} =~ m/$conf->{'database_regex'}/ )
) {
print "Skipping mail from=[". $message->{'sender_email'}."] subject=[". $message->{'subject'} ."]\n";
next MESSAGE;
}
exit 1;
my $database = $1;
my $backup_file = $conf->{'backup_path'} . $database .'-'. $date->ymd() .'.gz';
unless ( -f $backup_file ) {
die 'Cannot find backup file=['. $backup_file ."]\n";
}
print 'Restoring database=['. $database .'] from day=['. $date->ymd() .'] from file=['. $backup_file ."]\n";
# Restore DB
system( 'gunzip -c '. $backup_file .' | psql '. $database );
die "Error while restoring the database=[". $database ."] from file=[". $backup_file ."]" if ( $? >> 8 );
# Mark as read, add label, reply
$gmail->edit_labels(
'label' => 'Database fixes',
'action' => 'add',
'msgid' => $message->{'id'}
);
$gmail->send_message(
'to' => $conf->{'kumars_email'},
'subject' => 'RE: '. $message->{'subject'},
'msgbody' => "No problem. I've fixed it. \n\n Please be careful next time.",
);
$gmail->edit_labels(
'label' => 'unread',
'action' => 'remove',
'msgid' => $message->{'id'}
);
}

72
perl/smack-my-bitch-up.pl Executable file
View File

@ -0,0 +1,72 @@
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use SMS::Send;
use YAML;
# Config
my $conf = Load( <<'...' );
---
phone_numbers:
my_number: +15005550006
her_number: +xxx
reasons:
- Working hard
- Gotta ship this feature
- Someone fucked the system again
...
my $date = DateTime->now;
# Skip on weekends
if ( $date->day_of_week >= 6 ) {
exit;
}
# Exit early if no sessions with my username are found
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
my @sessions = grep {
m/$ENV{'USER'}/
} <$cmd_who>;
close $cmd_who;
exit if ( scalar( @sessions ) == 0 );
# Load Twilio API config
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
LINE: while ( my $line = <$env> ) {
next LINE unless ( $line =~ m/^(TWILIO[^=]+)=(.*)(?:[\n\r]*)/ );
$conf->{'env'}->{ $1 } = $2;
}
close $env;
# Randomize excuse
my $reason_number = int( rand( scalar( @{ $conf->{'reasons'} } ) ) );
my $sms_text = "Late at work. ". $conf->{'reasons'}[ $reason_number ];
# Create an object. There are three required values:
my $sender = SMS::Send->new('Twilio',
_accountsid => $conf->{'env'}->{'TWILIO_ACCOUNT_SID'},
_authtoken => $conf->{'env'}->{'TWILIO_AUTH_TOKEN'},
_from => $conf->{'phone_numbers'}->{'my_number'},
);
# Send a message to me
my $sent = $sender->send_sms(
text => $sms_text,
to => $conf->{'phone_numbers'}->{'her_number'},
);
# Did it send?
if ( $sent ) {
print "Sent message.\n";
} else {
print "Message failed.\n";
}