How to Backup and Restore UniFi data from the Command Line

Backup

backup the UniFi directory

tar zcvf unifi-backup.tgz /usr/UniFi/

Move the tar archive off site.

Restore

Upload the backup file to the server.

Note: You will need to have the UniFi controller install already.

Copy the archive to the root directory and then extract it.

mv unifi-backup.tgz /
cd /
tar zxvf unifi-backup.tgz

Reboot the server and login to the UniFi controller.

reboot

Upload ssh key to multiple servers automatically

Here is a quick script I created to automate copying a ssh key to multiple remote servers.

Basic command – the command uses sshpass to upload the ssh key to a remote server, this allows you to execute the command and not have to enter in a password to authenticate.

sshpass -p password ssh-copy-id -o StrictHostKeyChecking=no admin@remotehost

Script

#!/bin/bash

remotehosts="$1"
username="admin"
password="MyCoolPassword123"

for host in `cat ${remotehosts}`
do
sshpass -p${password} ssh-copy-id -o StrictHostKeyChecking=no ${username}@${host}
echo "Uploaded key to " ${host}
done

echo "Finished!"

 

Using the script

  1. Download here.
  2. Make it executable
    chmod +x sshcopy.sh
    
  3. Edit the script and change the username and password.
  4. Create a file that contains each host’s IP address or hostname.
  5. Run script (change hostlist.txt to your host list you created in step 3.)
    ./sshcopy.sh hostlist.txt
  6. Wait for the script to finish.

Example:

wget www.incredigeek.com/home/downloads/SSHCopy/sshcopy.sh
chmod +x sshcopy.sh
sed -i s/admin/bob/g sshcopy.sh                      <-- Change username - you can just manually edit the file,
sed -i s/MyCoolPassword123/password/g sshcopy.sh     <-- Change password - it might be easier than using sed
echo "192.168.1.100" >> host.txt                     <-- Add 192.168.1.100 to the host list
echo "Bob" >> host.txt                               <-- Add hostname bob to host list
./sshcopy.sh host.txt                                <-- Upload ssh key to all host's in the host file i.e. "bob" and "192.168.1.100"

How to Install a ZenPack in Zenoss 5

Log into your Zenoss server via ssh.

ssh root@zenossserver

Create working direcotry

 mkdir /tmp/zenpack && cd /tmp/zenpack/

Now upload the ZenPack to “/tmp/zenpack/” on your Zenoss server with your favorite ftp tool.

Stop the Zenoss service

serviced service stop zenoss.core

Restart the services needed to import the ZenPack i.e. the following.

ZooKeeper
mariadb
RabbitMQ
redis
zeneventserver
Zope

You can start all of them with the following commands

serviced service start ZooKeeper
serviced service start mariadb 
serviced service start RabbitMQ
serviced service start redis
serviced service start zeneventserver 
serviced service start Zope

Install ZenPack with the following command

serviced service run zope zenpack install ZenPack.xxx.xxx.egg

Restart Zenoss

serviced service restart zenoss.core

Log into Zenoss and make sure that it is working right.

XenServer Unable to Import OVF

Check to see if you have an import template.

In the Main Menu click “View” and check “Hidden Objects”, in the Server View you should see a Template named something like “XenServer Transfer VM 6.2.0-123456c (hidden)”

If you don’t have one, then run the following command on the XenServer to create a template.

/opt/xensource/packages/files/transfer-vm/install-transfer-vm.sh

If the command fails double check that you have a default storage repository selected and try again.

Once the template is created import the OVF.

How To “Unblock” an IP from cPHulk from the Command Line

cPHulk uses a MySQL database to keep track of different IP’s to block, white list, black list, etc.

When there have been X amount of failed login attempts from an IP, cPHulk adds an entry for the IP in the brutes table.

To “unblock” the IP we need to delete the entry.

open up the cPHulk MySQL database.

root@localhost [~]# mysql cphulkd

If your interested you can view all tables that cPHulk uses.

show tables;

Take a look at all the IP’s in the brutes table.

SELECT IP FROM brutes;

example:

mysql> SELECT IP FROM brutes;
+---------------+
| IP            |
+---------------+
| 30.134.41.221 |     <--  IP we want to unblock
| 31.134.40.251 |
+---------------+
2 rows in set (0.00 sec)

mysql>

To unblock the IP we just need to delete the row that has the IP address we want.

mysql> DELETE FROM brutes WHERE IP="30.134.41.221";

It should return the following.

mysql> DELETE FROM brutes WHERE IP="30.134.41.221";
Query OK, 1 row affected (0.00 sec)

mysql>

Exit MySQL.

mysql> exit
Bye
root@localhost [~]#

 

How to set a Static ip Address in OS X from Command Line

You can set a static ip address in OS X with the following command. Replace en0 with the appropriate interface.

sudo ipconfig set en0 INFORM 192.168.10.12

The default ethernet interface on a Mac is usually en0. You can run ifconfig to view all the interfaces available on the machine.

The ip address will last until you reboot.

How to Install an OS X .pkg file from Command Line

The following command installs the .pkg to the local hard disk.

installer -pkg /Users/username/Desktop/package.pkg -target LocalSystem

You can also replace “LocalSystem with a “/” they both do the same thing.

If you want to install the package on a different disk you can change the -target to the drives path.

installer -pkg package.pkg -target "/Volumes/Macintosh HD2"

You can find more about the command by typing “man installer” in the terminal.

How to Change, Add, and Delete Users in Linux

Changing a users name:

sudo usermod -l new-username old-username

To change the users directory name do the following.

 sudo usermod -d /home/new-username

You might need to log in as root or another user to successfully execute the commands.

Adding a user:

sudo useradd -m newuser

The -m option create the new users home directory

Then to activate the user we need to setup a password.

sudo passwd newuser

Enter and reenter the password and your done.

If you need to add the user to a certain group(s) (sudo?) you can do that with usermod

sudo usermod -G sudo,othergroups newuser

 

Deleting a user:

sudo userdel -r username

The “-r” option deletes the users mail and home directory.  If you wanted to keep them then just discard that option.

Installing Raspbian on Raspberry Pi from the Linux Command Line

First download the Rasbian zip from here.

Unzip the zipped file.

unzip 2014-01-07-wheezy-raspbian.img

You need to find your sdcard name.  If you don’t now how to, take a look at this post.

Next format your card as Fat32.

Now write the image to the sdcard with the following command.  Replace the mmcblk0 part of “of=/dev/mmcblk0” with your drive name.

sudo dd if=~/2014-01-07-wheezy-raspbian.img of=/dev/mmcblk0 bs=4M

It will not display any information until it is finished, so be patience.

Once it is complete, eject it and plug it into your Raspberry Pi and boot it up.

Vim Commands Cheat Sheet

This is a Vim (Vi IMproved) cheat sheet, listing some useful, essential and most often used Vim commands.  Most of the information was acquired here.

Vi is a command line text editor.  To use it, from command line type in vi followed by the file name you want to edit.

Example:

vi filename.txt

 

Working with files
Vim command Action
:e filename Open a new file. You can use the Tab key for automatic file name completion, just like at the shell command prompt.
:w filename Save changes to a file. If you don’t specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, specify the file name.
:q Quit Vim. If you have unsaved changes, Vim refuses to exit.
:q! Exit Vim without saving changes.
:wq Write the file and exit.
:x Almost the same as :wq, write the file and exit if you’ve made changes to the file. If you haven’t made any changes to the file, Vim exits without writing the file.
These Vim commands and keys work both in command mode and visual mode.
Vim command Action
j or Up Arrow Move the cursor up one line.
k or Down Arrow Down one line.
h or Left Arrow Left one character.
l or Right Arrow Right one character.
e To the end of a word.
E To the end of a whitespace-delimited word.
b To the beginning of a word.
B To the beginning of a whitespace-delimited word.
0 To the beginning of a line.
^ To the first non-whitespace character of a line.
$ To the end of a line.
H To the first line of the screen.
M To the middle line of the screen.
L To the the last line of the screen.
:n Jump to line number n. For example, to jump to line 42, you’d type :42
Inserting and overwriting text
Vim command Action
i Insert before cursor.
I Insert to the start of the current line.
a Append after cursor.
A Append to the end of the current line.
o Open a new line below and insert.
O Open a new line above and insert.
C Change the rest of the current line.
r Overwrite one character. After overwriting the single character, go back to command mode.
R Enter insert mode but replace characters rather than inserting.
The ESC key Exit insert/overwrite mode and go back to command mode.
Deleting text
Vim command Action
x Delete characters under the cursor.
X Delete characters before the cursor.
dd or :d Delete the current line.
Entering visual mode
Vim command Action
v Start highlighting characters. Use the normal movement keys and commands to select text for highlighting.
V Start highlighting lines.
The ESC key Exit visual mode and return to command mode.
Editing blocks of text
Note: the Vim commands marked with (V) work in visual mode, when you’ve selected some text. The other commands work in the command mode, when you haven’t selected any text.
Vim command Action
~ Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor.
> (V) Shift right (indent).
< (V) Shift left (de-indent).
c (V) Change the highlighted text.
y (V) Yank the highlighted text. In Windows terms, “copy the selected text to clipboard.”
d (V) Delete the highlighted text. In Windows terms, “cut the selected text to clipboard.”
yy or :y or Y Yank the current line. You don’t need to highlight it first.
dd or :d Delete the current line. Again, you don’t need to highlight it first.
p Put the text you yanked or deleted. In Windows terms, “paste the contents of the clipboard”. Put characters after the cursor. Put lines below the current line.
P Put characters before the cursor. Put lines above the current line.
Undo and redo
Vim command Action
u Undo the last action.
U Undo all the latest changes that were made to the current line.
Ctrl + r Redo.
Vim command Action
/pattern Search the file for pattern.
n Scan for next search match in the same direction.
N Scan for next search match but opposite direction.

Editor Commands

All these commands can be added to your vimrc file so they are always available when you  start vim.  To use them while in the editor, hit “esc then :”.

Enable Colorsheme (you can replace “default” with any of the themes installed. i.e. delek, elflord, evening, etc.)

colorscheme default

Turn Syntax On/Off

syntax on
syntax off

Highlight searched word

Note: you can also use # and * to search for the word your cursor is on.

Enable

set hlsearch

Disable

nohlsearch
noh