mirror of
https://github.com/narkoz/hacker-scripts
synced 2025-08-22 02:17:30 +00:00
initial commit
This commit is contained in:
commit
32b0128749
7
.env.example
Normal file
7
.env.example
Normal file
@ -0,0 +1,7 @@
|
||||
# Set these environment variables or edit and copy this file into `.env`
|
||||
# See: https://github.com/bkeepers/dotenv
|
||||
|
||||
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
TWILIO_AUTH_TOKEN=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
|
||||
GMAIL_USERNAME=admin@example.org
|
||||
GMAIL_PASSWORD=password
|
49
README.md
Normal file
49
README.md
Normal file
@ -0,0 +1,49 @@
|
||||
# DevOps Scripts
|
||||
|
||||
Based on a _[true
|
||||
story](https://www.jitbit.com/alexblog/249-now-thats-what-i-call-a-hacker/)_:
|
||||
|
||||
> xxx: OK, so, our build engineer has left for another company. The dude was literally living inside the terminal. You know, that type of a guy who loves Vim, creates diagrams in Dot and writes wiki-posts in Markdown... If something - anything - requires more than 90 seconds of his time, he writes a script to automate that.
|
||||
|
||||
> xxx: So we're sitting here, looking through his, uhm, "legacy"
|
||||
|
||||
> xxx: You're gonna love this
|
||||
|
||||
> xxx: `smack-my-bitch-up.sh` - sends a text message "late at work" to his wife (apparently). Automatically picks reasons from an array of strings, randomly. Runs inside a cron-job. The job fires if there are active SSH-sessions on the server after 9pm with his login.
|
||||
|
||||
> xxx: `kumar-asshole.sh` - scans the inbox for emails from "Kumar" (a DBA at our clients). Looks for keywords like "help", "trouble", "sorry" etc. If keywords are found - the script SSHes into the clients server and rolls back the staging database to the latest backup. Then sends a reply "no worries mate, be careful next time".
|
||||
|
||||
> xxx: `hangover.sh` - another cron-job that is set to specific dates. Sends automated emails like "not feeling well/gonna work from home" etc. Adds a random "reason" from another predefined array of strings. Fires if there are no interactive sessions on the server at 8:45am.
|
||||
|
||||
> xxx: (and the oscar goes to) `fucking-coffee.sh` - this one waits exactly 17 seconds (!), then opens an SSH session to our coffee-machine (we had no frikin idea the coffee machine is on the network, runs linux and has SSHD up and running) and sends some weird gibberish to it. Looks binary. Turns out this thing starts brewing a mid-sized half-caf latte and waits another 24 (!) seconds before pouring it into a cup. The timing is exactly how long it takes to walk to the machine from the dudes desk.
|
||||
|
||||
> xxx: holy sh*t I'm keeping those
|
||||
|
||||
Scripts are written in Ruby.
|
||||
Pull requests with other implementations (Python, Perl, Shell, etc) are welcome.
|
||||
|
||||
### Usage
|
||||
|
||||
Install required gems: `gem install dotenv twilio gmail whenever`
|
||||
Set environment variables. See `.env.example`
|
||||
|
||||
Example cron:
|
||||
|
||||
```sh
|
||||
# Runs `smack_my_bitch_up` daily at 9:20 am.
|
||||
20 21 * * * /bin/bash -l -c 'ruby smack_my_bitch_up.rb'
|
||||
|
||||
# Runs `kumar_asshole` every 10 minutes.
|
||||
45 8 * * * /bin/bash -l -c 'ruby hangover.rb'
|
||||
|
||||
# Runs `hangover` daily at 8:45 am.
|
||||
0,10,20,30,40,50 * * * * /bin/bash -l -c 'ruby kumar_asshole.rb'
|
||||
|
||||
# Runs `fucking_coffee` hourly from 9am to 6pm.
|
||||
9,10,11,12,13,14,15,16,17,18 * * * * /bin/bash -l -c 'ruby fucking_coffee.rb'
|
||||
```
|
||||
|
||||
Check `config/schedule.rb`.
|
||||
|
||||
---
|
||||
Code is released under WTFPL.
|
17
config/schedule.rb
Normal file
17
config/schedule.rb
Normal file
@ -0,0 +1,17 @@
|
||||
# http://github.com/javan/whenever
|
||||
|
||||
every :day, at: '9:20 pm' do
|
||||
command 'ruby smack_my_bitch_up.rb'
|
||||
end
|
||||
|
||||
every 10.minutes do
|
||||
command 'ruby kumar_asshole.rb'
|
||||
end
|
||||
|
||||
every :day, at: '8:45 am' do
|
||||
command 'ruby hangover.rb'
|
||||
end
|
||||
|
||||
every :hour, at: 9..18 do
|
||||
command 'ruby fucking_coffee.rb'
|
||||
end
|
17
fucking_coffee.rb
Executable file
17
fucking_coffee.rb
Executable file
@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Skip on weekends
|
||||
exit if Time.now.saturday? || Time.now.sunday?
|
||||
|
||||
require 'net/telnet'
|
||||
|
||||
coffee_machine_ip = '10.10.42.42'
|
||||
password = '1234'
|
||||
password_prompt = 'Password: '
|
||||
|
||||
con = Net::Telnet.new('Host' => coffee_machine_ip)
|
||||
con.cmd('String' => password, 'Match' => /#{password_prompt}/)
|
||||
con.cmd('sys brew')
|
||||
sleep 64
|
||||
con.cmd('sys pour')
|
||||
con.close
|
45
hangover.rb
Executable file
45
hangover.rb
Executable file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Skip on weekends
|
||||
exit if Time.now.saturday? || Time.now.sunday?
|
||||
|
||||
log_file_name = File.dirname(__FILE__) + '/logs/hangover.txt'
|
||||
|
||||
# Be sure that logs dir always exists
|
||||
Dir.mkdir('logs') unless File.exists?(log_file_name)
|
||||
|
||||
LOG_FILE = File.open(log_file_name, 'a+')
|
||||
|
||||
# Exit early if sessions with my_username are found
|
||||
exit unless `who`[/my_username/].nil?
|
||||
|
||||
require 'dotenv'
|
||||
require 'twilio-ruby'
|
||||
|
||||
Dotenv.load
|
||||
|
||||
TWILIO_ACCOUNT_SID = ENV['TWILIO_ACCOUNT_SID']
|
||||
TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN']
|
||||
|
||||
@twilio = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN
|
||||
|
||||
# Phone numbers
|
||||
my_number = '+xxx'
|
||||
number_of_boss = '+xxx'
|
||||
|
||||
excuses = [
|
||||
'Locked out',
|
||||
'Pipes broke',
|
||||
'Food poisoning',
|
||||
'Not feeling well'
|
||||
]
|
||||
|
||||
# Send a text message
|
||||
@twilio.messages.create(
|
||||
from: my_number, to: number_of_boss,
|
||||
body: 'Gonna work from home. ' + excuses.sample
|
||||
)
|
||||
|
||||
# Log this
|
||||
LOG_FILE.puts("Message sent at: #{Time.now}")
|
||||
LOG_FILE.close
|
30
kumar_asshole.rb
Executable file
30
kumar_asshole.rb
Executable file
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'dotenv'
|
||||
require 'gmail'
|
||||
|
||||
Dotenv.load
|
||||
|
||||
GMAIL_USERNAME = ENV['GMAIL_USERNAME']
|
||||
GMAIL_PASSWORD = ENV['GMAIL_PASSWORD']
|
||||
|
||||
gmail = Gmail.connect(username, password)
|
||||
|
||||
KEYWORDS_REGEX = /sorry|help|wrong/i
|
||||
|
||||
gmail.inbox.find(:unread, from: 'kumar.a@example.com').each do |email|
|
||||
if email.body[KEYWORDS_REGEX]
|
||||
# Restore DB and send a reply
|
||||
email.label('Database fixes')
|
||||
reply = reply_to(email.subject)
|
||||
gmail.deliver(reply)
|
||||
end
|
||||
end
|
||||
|
||||
def reply_to(subject)
|
||||
gmail.compose do
|
||||
to "email@example.com"
|
||||
subject "RE: #{subject}"
|
||||
body "No problem. I've fixed it. \n\n Please be careful next time."
|
||||
end
|
||||
end
|
43
smack_my_bitch_up.rb
Executable file
43
smack_my_bitch_up.rb
Executable file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Skip on weekends
|
||||
exit if Time.now.saturday? || Time.now.sunday?
|
||||
|
||||
log_file_name = File.dirname(__FILE__) + '/logs/smack_my_bitch_up.txt'
|
||||
|
||||
# Be sure that logs dir always exists
|
||||
Dir.mkdir('logs') unless File.exists?(log_file_name)
|
||||
|
||||
LOG_FILE = File.open(log_file_name, 'a+')
|
||||
|
||||
# Exit early if no sessions with my_username are found
|
||||
exit if `who`[/my_username/].nil?
|
||||
|
||||
require 'dotenv'
|
||||
require 'twilio-ruby'
|
||||
|
||||
Dotenv.load
|
||||
|
||||
TWILIO_ACCOUNT_SID = ENV['TWILIO_ACCOUNT_SID']
|
||||
TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN']
|
||||
|
||||
@twilio = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN
|
||||
|
||||
# Phone numbers
|
||||
my_number = '+xxx'
|
||||
her_number = '+xxx'
|
||||
|
||||
reasons = [
|
||||
'Working hard',
|
||||
'Gotta ship this feature',
|
||||
'Someone fucked the system again'
|
||||
]
|
||||
|
||||
# Send a text message
|
||||
@twilio.messages.create(
|
||||
from: my_number, to: her_number, body: 'Late at work. ' + reasons.sample
|
||||
)
|
||||
|
||||
# Log this
|
||||
LOG_FILE.puts("Message sent at: #{Time.now}")
|
||||
LOG_FILE.close
|
Loading…
x
Reference in New Issue
Block a user