Tyrel's Blog

Code, Flying, Tech, Automation

Jan 09, 2015

SSH Agent on "boot"

I had a friend complain that he had to keep adding his ssh key to his ssh-agent every time he rebooted. I have a really easy bit of shell code you can put into your .bashrc or your .zshrc file:

SSHKEYFILE=/path/to/your/ssh/key/file
ssh-add -l | grep -q $SSHKEYFILE
RC=$?
if [ $RC -eq 1 ]
then
    ssh-add -t 86400 $SSHKEYFILE
echo "Added key internal to keychain."
fi

This will check every time your bash or zsh rc file is sourced for your key in the currently added keys, and if it's not, it will add it.

This has the benefit of not requiring you to put in your password every time you connect to a remote machine if you password your ssh keys (which you should).

 · · ·  linux  ssh

Jun 21, 2014

Readline

A lot of times when I stop at someone's computer and help them in the terminal, I use a Readline command and people say "How the heck did you do that?"

Let me first backup and explain what Readline is. From the GNU Readline Documentation - "The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in." By default, Readline is set up in Emacs mode, no you don't have to have an extra four fingers to use Readline, most of the commands are simple.

Here are a couple of the commands I use daily:

Movement

  • To move to the beginning of a line, you press C-a
  • To move to the end of a line you press C-e

Killing and Yanking

  • To cut the rest of the line from where your cursor is, to the end, you press C-k
  • To delete one word you press C-w
  • To paste either of the two previous back you can press C-y

Miscellaneous

  • To clear the screen and get to a fresh start, you can press C-l
  • To end your session you can send a C-d (This will send an end of file character)
  • To search for a command you typed recently, press C-r and start typing, it will search backwards. C-r again will search for an earlier match.
  • The inverse of C-r is C-s, they function the same.
  • To open your $EDITOR to edit the current shell command you wish to write, press C-x C-e

Finally, don't forget about C-c. While not specifically Readline, it's very useful because it sends the SIGINT signal to the program, which if just on the command line, will not execute the line you have type, and give you a new line with nothing on it. A nice clean start.

To find out a lot more, read the documentation at the Readline Commands Docs I even learned some things while writing this up, apparently pressing C-x $ will list off all the possible usernames. Good to know, and good to always keep learning.

 · · ·  readline  linux  cli

Mar 08, 2012

Some BASH tips

I realize I haven't updated in a while. I haven't had much free time recently as I've been working on a project for my father in C# after work hours. This is a great change from only working in Python and JavaScript recently. I'm making a program that will analyze test results from a plasma torch for a company called HyperTherm. My father built the physical machine, but the employees want something that they can better see the results of a passed torch unit, or a failed torch unit. This program has a bar code scanner that scans the tool used in the test and matches it up to the lot of torch parts. Another added feature is the ability to print a white label that says "UNIT PASSED" or a giant red label that says the unit failed and which of the 8 tests failed were.I had to learn how to use delegates, as my serial event listener is on a separate thread and I can't update labels, or parts of the User Interface without them. Still working on it, hopefully will wrap it up by Saint Patrick's day.

I recently found a cool command in BASH that I hadn't previously known. C-o will execute the current line, and then bring the following line up from BASH history. If you have a set of commands you want to execute again, rather than having to press up 20 times, hit enter, press up 19 times, hit enter, and so on… You can just hit up 20 times. Press C-o as many times as you need to.

For example:

$ touch a
$ touch b
$ touch c
# [up] [up] [up]
$ touch a [C-o]
$ touch b [C-o]
$ touch c [C-o]

As you can see there, all I had to do was go back to the $ touch a line, and hit control-o three times and it touched the files again!

 · · ·  bash  linux

Jan 13, 2012

You can un-expire a GPG key.

Today we had a problem at work on a system. Without getting into too much detail as to give away secrets behind the verbal NDA I am behind, I will just say that it had to do with a GPG public key of mine that was expired on a dev machine, accidentally propagating during install to a production machine. This key had a sub key as well, so figuring out this was tricky.

To start, you can list your gpg keys like so:

$ gpg --list-keys

This will list keys such as

pub 4096R/01A53981 2011-11-09 [expires: 2016-11-07]
uid Tyrel Anthony Souza (Five year key for email.)
sub 4096R/C482F56D 2011-11-09 [expires: 2016-11-07]

To make this not expire, (same steps to change expiration date to another time), you must first edit the key

$ gpg --edit-key 01A53981

You will then see a gpg prompt gpg>

Type "expire" in and you will be prompted for how long to change it to

Changing expiration time for the primary key.
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years

You are then done setting the expiration on the primary key, if you have sub key, doing this is as easy as typing key 1 and repeating the expiration step.

To finish and wrap things up, type save and you are done.

 · · ·  linux  gpg
← Previous Page 2 of 2