Category: Tricks and Cheats

  • Setup SSH Tunnel for SytemD

    The what:

    There are several cases when you just need some SSH tunnel to another system with some port redirections.
    If the tunnel fails for whatever reason, it should reset itself. Setting up a SSH tunnel with port redirection as a service is the answer!

    The How:

    Creating SystemD config

    The setup is a “user defined” config. When the user is logged on to the system, the service will become active.
    You need to setup SSH password less login setup (with keys) to be able to use this.

    First you need to created some directories (if not present)

    mkdir -p ~/.config/systemd/user

    Then create a SystemD service definition file inside this location
    Name the file:

    ssh_tunnel@.service

    The content….:

    # Author: VDV-IT Consultancy
    # URL: https://www.vdv-it.nl
    [Unit]
    Description=Setup a secure tunnel to %I
    After=network-online.target

    [Service]
    ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes %i

    # Restart every >2 seconds to avoid StartLimitInterval failure
    RestartSec=5
    Restart=always

    [Install]
    WantedBy=default.target

    After creating the file, SystemD needs to know that this file is present. You need to do a reload of SystemD

    systemctl --user daemon-reload

    Almost done…..

    Define the tunnels

    To define a tunnel, add the following lines to your ~/.ssh/config file. If not present, create the file.

    Host [Your tunnel name]
    HostName [ip or url to remote computer]
    Port [ssh port, usually 22]
    User [username for login]
    IdentityFile ~/.ssh/[the private key to be used].key
    LocalForward [local port to listen] localhost:[remote port to connect to]
    RemoteForward [remote port to listen] localhost:[local port to connect to]

    More tunnels? just duplicate the block with other params.
    If you need more option for SSH, take a look at man ssh_config

    Running the tunnels

    Lets get your hard work up and running!
    Use the systemctl command to start/stop/status you’re tunnel

    systemctl --user start ssh_tunnel@[your tunnel name]

    To set-up autostart, use the same command but replace start with enable
    This will start the tunnel when you log-on to your system.

    systemctl --user enable ssh_tunnel@[your tunnel name]

    Thats it! you’re up and running!

    Checking / Stopping the tunnel

    For status checking, use the systemctl command

    systemctl --user status ssh_tunnel@[Your tunnel name]

    To stop the tunnel

    systemctl --user stop ssh_tunnel@[Your tunnel name]

     

     

  • MySQL Search and Replace Query

    You need to change some data or whatever inside a table, but you don’t want to edit every row by row. Just use this query to search for a string in a table column and change it!

    UPDATE  `MySQL_Table` SET  `MySQL_Table_Column` = REPLACE(`MySQL_Table_Column`, 'oldString', 'newString') WHERE  `MySQL_Table_Column` LIKE 'oldString%';

    When Should I use MySQL Search and Replace?

    Doing a Search and Replace via SQL could be dangerous if you have links that you’re unaware of which shouldn’t be changed. You should Search and Replace only when you’re convinced that you have to change the text in your Database. You should always perform a Database backup before proceeding with any manual changes. This can easily be done through phpMyAdmin:

  • Search Puppet file bucket for a changed file(s)

    When you use Puppet to manage your infrastructure, it’s common to manage several files on a system with a template or something. The default setting when a file is changed, it gets a backup on the client system in a filebucket

    You can use to command ‘puppet filebucket’ with a lot of options to hopefully get what you want…. Or…. just search the filesystem 🙂

    The location below holds the directory structure were the files are physically stored! with a lot of hashes added 😐

    '/opt/puppetlabs/puppet/cache/clientbucket/'

    A directory structure could look like this:

    f
    f/8
    f/8/6
    f/8/6/3
    f/8/6/3/1
    f/8/6/3/1/b
    f/8/6/3/1/b/a
    f/8/6/3/1/b/a/a
    f/8/6/3/1/b/a/a/f8631baa8f83d5067af7eedcd7bdf641
    f/8/6/3/1/b/a/a/f8631baa8f83d5067af7eedcd7bdf641/contents
    f/8/6/3/1/b/a/a/f8631baa8f83d5067af7eedcd7bdf641/paths

    The files content and paths hold the magic information about original file and its location. YES!!

    Use the following command the search your filebucket to get you’re long lost files back!!

    find /opt/puppetlabs/puppet/cache/clientbucket/ -name paths -print -exec grep <file to look for> {} \;

    This only works when the file is managed through the ‘file’ resource type.

    Files changed by augeas or not to be found here…. Bummer!

  • Bourne-Again SHell (bash) cheat sheet

    Open your eyes to world and enjoy linux!

    This sheet tells you all the funky stuff  bash does for you!

    The Bash Cheat Sheet

     

  • Rename all files in directory from $file to $newfile

    The what?

    Dead simple.
    How do I rename

    hello_kitty2.png
    hello_kitty3.png

    to

    byebye_kitty2.png
    byebye_kitty3.png

    I think it’s simple, but it’s hard to Google for this kind of thing unless you already know.

    The how…..!

    You can do this with a simple one-liner with bash

    for file in $(ls hello_ki*); do mv "$file" "${file/hello/byebye}"; done