Automate SSH logins when a password is required

Today I learned how to automate SSH logins when a password is required. In order to make this happen we need to first install expect

Install Expect

brew install expect

Create bash scripts

cd /usr/bin/local
touch exp
touch ssh-custom
chmod u+x exp ssh-custom

Open the exp file and enter in the following:

#!/usr/bin/expect -f
set timeout 20

set cmd [lrange $argv 0 end]

# Check if SSHPASS is set
if {[info exists env(SSHPASS)]} {
    set password $env(SSHPASS)
} else {
    puts "Error: SSHPASS environment variable not set."
    exit 1

spawn ssh $cmd
expect "password for your-username:"
send "$password\r"

The string on the line expect "password for your-username:" will need to match the text that gets displayed when you ssh normally to whatever server is requiring a password. This will be what expect waits for before entering in your password.

Now open the ssh-custom file and enter the following. This will be what we eventually setup an alias for that will divert the ssh command to the exp file that will handle servers that require a password.


if [ "$1" == "production" ]; then
    exp production
    # Default behavior for other SSH commands
    ssh "$@"

Now we need to setup the environment variable and alias. Assuming you use zsh, add the following to your .zshrc file.

alias ssh="ssh-custom"
export SSHPASS="yoursshpassword"


Now when you run ssh production it will automatically enter your password.

How to bypass default_scope

Today I learned how to bypass default_scope using ActiveRecord::QueryMethods#unscope. And change a previously set where condition.

class Products
  default_scope where(enabled: true)
  scope :disabled, -> { unscope(enabled: false) }

Another helpful method is ActiveRecord::QueryMethods#rewhere

class Products
  default_scope where(enabled: true)

product = Products.first
product.rewhere(enabled: false)

Force string to boolean object


You have a report that expects a boolean. Sometimes the it is run from a job that passes in a boolean. And sometimes it is executed from a controller using a param that comes in as "true" or "false".

How do you ensure "true" and true are both treated as a boolean?


Force string to boolean using

=> true

=> true

Force Rails Database Migration


Let’s say you create the following Rails migration forgetting to add code to the change method. You then issue the rails migration command rails migrate

class AddAwesomeToStuff< ActiveRecord::Migration[6.0]
  def change

You then update the migration with your desired changes and try issuing the migrate command again. Nothing happens. It is because each time a migration runs the version number is stored in a table that prevents the same migration running more than once.

class AddAwesomeToStuff< ActiveRecord::Migration[6.0]
  def change
    change_table :stuff do |t|
      t.string :awesome


Force a migration to run in the Rails console

irb(main)> require "#{Rails.root.to_s}/db/migrate/20211004223636_add_awesome_to_stuff.rb"
irb(main)> AddAwesomeToStuff.migrate(:up)

Clean up RSpec Global Namespace

Today I learned (after reading this article) that in RSpec declared classes, modules, and structs are global. They pollute the global namespace and can cause order dependent spec failures. You can read up more on the topic here.

To avoid this you can this to your rails_helper.rb

RSpec.configure do |config|
  config.around(:example, remove_const: true) do |example|
    const_before = Object.constants

    const_after = Object.constants
    (const_after - const_before).each do |const|
      Object.send(:remove_const, const)

Simplifying code using Rails presence Method

Rails has a useful method called presence that allows you to use the value of an object, otherwise it returns nil.

It is very simple. Below is the implemenation.

  def presence
    self if present?

We can use it to refactor the following code

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

to this

region = params[:state].presence || params[:country].presence || 'US' 

Detect iframe click events with pointer-events

Today I learned about the CSS attribute pointer-events. Used with an iframe and parent div you can detect when a click has been made over an embedded iframe.

<div class="container">
iframe {
  pointer-events: none;
$(document).ready(function () {
  $('.container').on('click', function() {
    $(this).after("<p>click detected!</p>");