Creating a SSH Tunnel

Posted by | Posted in Command-Line, Guides, Linux, OS X, Server, SSH, Tunneling, Ubuntu | Posted on 01-02-2011

To traverse firewalls that block incoming SSH connections or to access a computer with a non routable IP address, you can create an SSH tunnel. When creating a SSH tunnel, you’ll generate the tunnel on the host system and connect it to another system.

With the SSH command, you’ll use the “-R” flag, that allows reversible communications or a reverse tunnel.

This command that I normally use:

It states, create a reversible SSH tunnel from this localhost port 22 to USER@SERVER on port 10000.

 
/usr/bin/ssh -R 10000:localhost:22 USER@SERVER

You’ll need to leave this connection open otherwise the tunnel will collapse. Normally I execute a program at the other end to keep the connection alive. Some SSHd configurations will drop connections due to inactivity.

To connect to the tunnel from USER@SERVER:

use ssh to connect to your USER account on the host server via port 10000.

 
/usr/bin/ssh USER@localhost -p 10000

this will give you the login prompt at the host server. User your login information or you can also setup SSH Keys.

How to get rid of cron warnings

Posted by | Posted in Bash, Code, Command-Line, Cron, Linux, OS X, Server, Ubuntu | Posted on 31-01-2011

If a cron job executes with unmanaged warnings for data, you’ll generate LOTS of emails to your account. Here is an easy way to redirect all that extraneous data.

Just add “>> /dev/null 2>$1″ to the end of each of your cron jobs.

 
0,10,20,30,40,50 * * * * /path/to/script >> /dev/null 2>&1

Executing more then 1 command at once

Posted by | Posted in Bash, Code, Command-Line, Cron, Linux, OS X, Server, Ubuntu | Posted on 30-01-2011

Many times, I have to execute sequential commands inorder to complete a task. Below is an example of how to do this.

/path/to/command argv1 && /path/to/command argv2

I normally use this sequential order when pulling raw data from a source and then reformatting it for my needs.

15 * * * * /usr/local/bin/pullsource grab && /usr/local/bin/pullsource reformat

Applescript – folder and file counter

Posted by | Posted in Applescript, Code, OS X | Posted on 16-09-2010

set theFolder to (choose folder)
set thePath to POSIX path of theFolder
set fileCount to 0
set folderCount to 0
tell application "Finder"
	set theFileList to every item in folder theFolder
	repeat with x in theFileList
		if not character 1 of ((name of x) as string) is equal to "." then
			if kind of x is equal to "Folder" then -- "folder" depends fromĀ  the finder language 
				set folderCount to folderCount + 1
			else
				set fileCount to fileCount + 1
			end if
		end if
	end repeat
end tell
 
display dialog "There are " & folderCount & " folders andĀ  " & fileCount & " files in " & thePath

Applescript – Change folder color based on size

Posted by | Posted in Applescript, Code, OS X | Posted on 16-09-2010

property theFile : missing value
property theFolder : "test"
property thePosixPath : "/Users/user/Desktop/"
property theAliasPath : "Users:USER:Desktop:"
property theSize : missing value
 
tell application "Finder"
	set theSize to do shell script "du -s -k " & thePosixPath & theFolder & " |awk '{print $1}'"
	if theSize > 700000 then
		set label index of (folder theAliasPath & theFolder) to 2
	end if
end tell

Randomly Choose A Buddies’ Status Message As Your Own – OS X – Applescript

Posted by | Posted in Applescript, Code, OS X | Posted on 10-11-2009

tell application "iChat"
tell application "iChat" to set messagelist to get status message of every buddy
where status is not offline
set msglistcount to count messagelist
set theMessageList to {}
repeat with x from 1 to msglistcount
if item x of messagelist is not "" then
copy item x of messagelist to end of theMessageList
end if
msglistcount = msglistcount - 1
end repeat
set newStatus to some item of theMessageList
set status message to "i'm in your internets stealing your status messages : " & newStatus
end tell

Syncing Date and Time with a NTP Server – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 23-09-2009

If you have several servers to manage, having them set with the same date and time is very important.

NTP stands for Network Time Protocol.

ntpdate – set the date and time via NTP

sudo ntpdate NTPServerAddress

or if you want to use ubuntu’s time server.

sudo ntpdate ntp.ubuntu.com

If you want to use multiple time servers, you can use a command like this. Where the new time server follows the address of your current server.

sudo ntpdate ntp.ubuntu.com time.apple.com

Links:
NTP Pool Project
Ubuntu Time Synchronization with NTP

How to download files via command line – OS X, Ubuntu : Wget, cURL

Posted by | Posted in Command-Line, Linux, OS X, Ubuntu | Posted on 21-07-2009

For most server administrators doing system updates or installing patches via a command shell is normal, but for the average user trying to patch his new system, this task might be a little difficult. Opening a terminal and downloading a file, just by typing in a few words, is not as simple as clicking a download icon on a website, tho, once you learn how to use Wget or cURL, it will be.

From my understanding, Wget is a stand-alone application, that is command-line only. And, cURL is a cross-platfrom API library called libcurl. Personally, I like Wget better then cURL, but that’s just because I am bias.

If you want to know all the nitty-gritty differences, check out curl vs Wget.

Wget – The non-interactive network downloader. (I refer to it as, WWW Get or Web Get).

wget [option]… [URL]…

curl – transfer a URL

curl [options] [URL...]

How to download a file with wget:

wget http://domain.com/path/to/file.zip

How to download a file with cURL:

curl -O http://domain.com/path/to/file.zip
 
or
 
curl http://domain.com/path/to/file.zip > file.zip

Using PHP to return an IP address

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

After I’ve determine which interfaces are active on my computer(s), I can use this script to report back the IP address of said interface.

# input String ("en0")
# output String IP
function get_network_interface_ip($interface) {
  @exec('/usr/sbin/ipconfig getifaddr '. $interface, $retval);
 return $retval[0];
}// end get_network_iterface_ip
echo get_network_interface_ip($interface);

Using PHP to report an active network interface

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script, after I determine what available interfaces the computer has, to report which interfaces are active.

# input String ("en0")
# output "NULL or "Active" 
function check_interface_activity($interface){
  @exec('ifconfig| awk \'/flags=|media|inet / {if (substr($2, 1, 6) == "flags=") printf("\n%s ", $1);
  else if ($1 == "inet") printf("%s ", $2);
  else if ($1 == "media:") printf(substr($0, 9))}\' | awk \'$1 ~ /^'.$interface.'/\' | awk \'{print $7}\'',$retval);
 return $retval[0];
} // end check_interface
echo check_interface($interface);

Join the mailing list

Check your email and confirm the subscription