🕹ī¸ Do Something Great! 😄

Category: Geek

  • Searching bash history when it’s in multiple files

    When I look for previous commands that I have ran in bash, I use history | grep COMMAND. That works well when you’re history is in one file, but I now save my history in multiple files from each shell and my simple command doesn’t work.

    With my history files saved in ~/.bash_history_log/ I needed a different solution. The one I came up with is grep -ri COMMAND ~/.bash_history_log/*. That seems awfully long, lets make it shorter. You can’t pass arguments to bash aliases, but you can to functions. In .bash_profile (or .bashrc, which ever runs for you by default) I added the following function:

    function searchhistory() {
        grep -ri "${1}" ~/.bash_history_log/*
    }
    alias shistory=searchhistory
    

    And now a simple shistory COMMAND is all it takes to find that command I ran a long time ago.

  • Uploading my current IP address so I can find my Pi

    For some reason my autossh job is failing to set up the reverse tunnel so I can find and connect to my Pi when the Pi is roaming. Until I figure out why that is happening, I wrote this bash script which will upload the wifi interface’s current IP address to a remote server.

    1
    2
    3
    4
    #!/bin/bash
    
    echo `ifconfig wlan0 | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'` | \
        ssh [email protected] "cat > ~/myip.txt"
    

    I already have ssh passwordless logins in set up because of autossh. If you don’t have that set up, then you’ll need to do that first or this won’t work. I then add a cron job to run this script every minute.

  • Upload and converting pictures automatically from Dropbox

    I’ve been using Pelican to publish this website for awhile now, and one of the things I wanted to do was to be able to post from my phone and include pictures. To accomplish this I wrote a script that checks the upload folder in dropbox, and if there is a file, it will re-size the pictures to 4 different sizes and upload it to Amazon S3. I can then easily link to any of the sizes of pictures for my posts.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    #!/bin/bash
    
    TOUPLOAD=/Path/to/check/for/pics/*
    SAVEUPLOADED=/Path/to/save/uploaded/pics
    S3BUCKET="BUCKET/for/pics"
    S3CMD=/usr/local/bin/s3cmd
    DIR=/Path/to/html/file/that/has/links/to/pics
    UPLOADED=FALSE
    
    
    if [[ ! -d /tmp/uploadpic ]]; then
        mkdir /tmp/uploadpic
    fi
    
    if [[ ! -d ${UPLOADED} ]]; then
        mkdir -p ${UPLOADED}
    fi
    
    shopt -s nullglob
    
    for f in ${TOUPLOAD}
    do
        if [[ -f ${f} ]]; then
        OUT=/tmp/uploadpic
        FILENAME=${f}
        FULLFILENAME=$(basename "${f}")
        EXT=${FULLFILENAME##*.}
        FILE=${FULLFILENAME%.*}
    
        echo "Working with ${FILE}.${EXT}"
    
        echo "Resizing"
        convert "${FILENAME}" -resize 320x240 "${OUT}/${FILE}-xs.${EXT}"
        convert "${FILENAME}" -resize 640x480 "${OUT}/${FILE}-s.${EXT}"
        convert "${FILENAME}" -resize 800x600 "${OUT}/${FILE}-m.${EXT}"
        convert "${FILENAME}" -resize 1024x768 "${OUT}/${FILE}-l.${EXT}"
        convert "${FILENAME}" -resize 2048x1024 "${OUT}/${FILE}-x.${EXT}"
    
        echo "Uploading"
    
        find ${OUT} -name "${FILE}*" -print0 | xargs -0 -I upload ${S3CMD} put upload s3://${S3BUCKET}/ --acl-public
    
        ${S3CMD} put "${FILENAME}" s3://${S3BUCKET}/ --acl-public
        mv "${FILENAME}" ${SAVEUPLOADED}/
        UPLOADED=TRUE
    fi
    done
    
    if [[ ${UPLOADED} = TRUE ]]; then
        echo "Pics have been uploaded..."
        echo "<html><head><title>Blog Pics</title></head><body><ul>" > ${DIR}
        ${S3CMD} ls s3://${S3BUCKET}/ | sort -r | cut -c 69- | xargs -I filename echo "<li><a href='http://${S3BUCKET}/filename'>filename</a>" >> ${DIR}
        echo "</ul></body></html>" >> ${DIR}
    fi
    

    This requires ImageMagick to be installed for the convert command. Once I have the script written I set up crontab to run the script every minute.

  • Some fun with ST Writer on the Atari MegaSTe

    Inspired by a thread on AtariAge, I decided to write up my blog posts tonight on my Atari MegaSTe in ST Writer.

    The Atari MegaSTe was released near the end of the Atari computer era with a 68000 cpu running at 16MHz in a case that separates the keyboard with the computer itself. I’m using the Atari SC1224 color monitor with it (12″ at 640×200 resolution) which is fine for most things. The mono monitor is a superb 640×400 resolution at 70Hz, but mine needs some adjusting since the screen is too small. The keyboard is pretty amazing, one of the best feeling keyboards I have every used.

    ST Writer is the word processor that is the heir apparent to the AtariWriter word processor for Atari’s 8-bit line of computers. I used AtariWriter for at least 6 years as my word processor, and it made the most out of the 40 character wide screen. ST Writer feels like coming home, although now with 2.5MB of free space.

    To post the article, I will use ZModem to transfer it to my Linux box where I can fix the line endings and can then use Pelican to post.

    It’s pretty weird to be back on a single processing system with limited Internet access. I should do this more often!

  • autossh problems

    My Pi is doing a pretty good job on connecting to wifi when I travel between places. Unfortunately, my autossh job on the Pi is not working.

    I set up autossh to open up a reverse ssh tunnel to a known host, allowing me to always ssh to my Pi using a hostname. But it seems like I need to do some more testing.

  • Finding your Pi on the network

    When you’re playing around with the Raspberry Pi, there are multiple times when you won’t have a screen attached to the Pi but you’ll want the IP address to use with SSH. What I did was set up static IP addresses on my DHCP server (in this case the ASUS RT-AC66U$).

    Click on LAN on the left side, and then select the DHCP tab:

    Manual IP Management

    Enter the MAC address of the network port on the Pi and select an IP address that isn’t being used. You’ll probably want to enter the MAC address of the wifi dongle too, if you use that. The ASUS is nice because the drop down will contain the MAC addresses of the devices that have connected to it, so you should be able to go through that list until you find the correct entries. To get the MAC addresses if this method doesn’t work, ssh to the pi and run ifconfig | grep HWaddr. It will then show the network interfaces and their MAC addresses.

    Once you have it set up, it’s really nice playing around with your Pis. It doesn’t matter what SD card you put in, as long as the Pi is set for DHCP, it will always be assigned the IP address you set.

    $ – Affiliate link to Amazon. Non-affiliate link.

  • A bash script to check domain expirations

    I have a bad habit, and that’s of buying domain names. I have about 20, with several different registrars. If I was smart, I would consolidate them under one registrar, but, even then, checking expirations on the domains is a pain. I usually have autorenew turned on, but I still like to know when they are getting close to expiration.

    Before re-inventing the wheel, I did a Google search, and found Domain Expiration Check Shell Script. It worked pretty well, except for a few top level domains like .me which didn’t work. I modified the script, and just had to put some finishing touches on it today (.com, .net, and .org was broken). Check out the gist of the script on Github.

    I put all of my domains in a text file, and can then run it with ./domain-check -f mydomains.txt. It’s pretty cool.

  • Set your Raspberry Pi up for Wifi roaming

    I’ve had a Raspberry Pi 2 just sitting around for several months, waiting for a purpose. Since I haven’t come up with a purpose, I decided to make it a portable headless Linux box that will travel with me, connect to Wifi automatically, and eventually, hopefully, set it up as a Piratebox. First things first, lets get it connecting.

    It was easier than I thought it was going to be to set it to automatically connect. Basically, I modified /etc/network/interfaces to add a roam configuration for wpa supplicant.

    # Include files from /etc/network/interfaces.d:
    source-directory /etc/network/interfaces.d
    
    auto lo
    iface lo inet loopback
    
    iface eth0 inet manual
    
    allow-hotplug wlan0
    iface wlan0 inet manual
        # wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
        wpa-roam /etc/wpa_supplicant/wpa_roam.conf
    
    allow-hotplug wlan1
    iface wlan1 inet manual
        #wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
        wpa-roam /etc/wpa_supplicant/wpa_roam.conf
    

    I just added the two lines that start with wpa-roam and commented out the default wpa_supplicant.conf lines. Next up was to create the /etc/wpa_supplicant/wpa_roam.conf file:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    network={
            ssid="home"
            key_mgmt=NONE
    }
    network={
            ssid="iPhoneHotSpot"
            key_mgmt=WPA-PSK
            psk="PASSWORD"
    }
    

    Adjust the parameters for your networks. Plugging the Pi into the network and manually running sudo ifdown wlan0;sudo ifup wlan0 is how I tested it.

    Finally, the pi needs to see if it doesn’t have a network connection, and if it doesn’t, bring wlan0 and then back up and see if it will connect. My wifi dongle (the Edimax EW-7811Un) is on wlan0, so you may need to modify. I set a crontab (sudo crontab -e) with the following line:

    * * * * * /bin/ping -q -c2 8.8.8.8 || (/sbin/ifdown --force wlan0 ;/sbin/ifup wlan0 )
    

    Every minute the Pi will send out two pings to Google’s DNS servers, and if it doesn’t get a reply, it will take the connection down and bring it back up. Now I have a Pi that will autoconnect to wifi.

  • Fixing my .taskpaper reset alias for bash

    There was a little problem with my alias to reset a .taskpaper list by removing all of the @dones. It didn’t remove any spaces before the @done, so each line would gain a space every day. This fixes it:

    alias rst="sed -i '' 's/ *@done\(.*\)//g'"
    

    Now it removes one or more spaces that could be before @done.

  • Switching terminals in tmux with ALT-numbers

    I’m a big tmux fan, switching from screen a few years ago. Lately, I’ve been working on ways to switch between terminals faster. Now I’m using the ALT (Option key actually) under OS X to switch between the different terminals. The switching starts with the backtick and goes across the upper row of the keyboard until the equal key for 12 terminals, and then continues on with the second row and the Q key up until the I key for terminal 20. It seems to be working pretty well, the biggest issue is that I can’t jump to specific buffers in Weechat with ALT-BUFFER#, but that’s not a big deal (I’m using ALT-J BUFFER# right now.) Here’s the specific section of my .tmux.conf file:

    # Use ALT-n keys to select pane
    bind-key -n M-"`" select-window -t 0
    bind-key -n M-1 select-window -t 1
    bind-key -n M-2 select-window -t 2
    bind-key -n M-3 select-window -t 3
    bind-key -n M-4 select-window -t 4
    bind-key -n M-5 select-window -t 5
    bind-key -n M-6 select-window -t 6
    bind-key -n M-7 select-window -t 7
    bind-key -n M-8 select-window -t 8
    bind-key -n M-9 select-window -t 9
    bind-key -n M-0 select-window -t 10
    bind-key -n M-- select-window -t 11
    bind-key -n M-= select-window -t 12
    
    bind-key -n M-q select-window -t 13
    bind-key -n M-w select-window -t 14
    bind-key -n M-e select-window -t 15
    bind-key -n M-r select-window -t 16
    bind-key -n M-t select-window -t 17
    bind-key -n M-y select-window -t 18
    bind-key -n M-u select-window -t 19
    bind-key -n M-i select-window -t 20