<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lennart&#039;s weblog &#187; SSH</title>
	<atom:link href="http://blog.karssen.org/tag/ssh/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.karssen.org</link>
	<description>Open source, computers, Africa and other more (or less) interesting stuff.</description>
	<lastBuildDate>Thu, 02 Feb 2012 19:15:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Using rsync to backup to a remote Synology Diskstation</title>
		<link>http://blog.karssen.org/2011/02/06/using-rsync-to-backup-to-a-remote-synology-diskstation/</link>
		<comments>http://blog.karssen.org/2011/02/06/using-rsync-to-backup-to-a-remote-synology-diskstation/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 20:54:36 +0000</pubDate>
		<dc:creator>LCK</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://blog.karssen.org/?p=108</guid>
		<description><![CDATA[I recently bought a NAS, a Synology DiskStation DS211j and stuffed two 1TB disks in it. I configured the disks to be in RAID 1 (mirrored) in case one of them decides to die. I then brought the NAS to a family member&#8217;s house and installed it there. Now she uses it to back up [...]]]></description>
			<content:encoded><![CDATA[<p>I recently bought a NAS, a <a href="http://www.synology.com/nld/products/DS211j/index.php"> Synology DiskStation DS211j</a> and stuffed two 1TB disks in it. I configured the disks to be in RAID 1 (mirrored) in case one of them decides to die. I then brought the NAS to a family member&#8217;s house and installed it there. Now she uses it to back up her important files (and as a storage tank for music and videos).</p>
<p>The good thing for me is that I can now make off-site backups of my home directories. I configured the DS211j to accept SSH connections so that I can log into it (as user admin or root). I used the web interface to create a directory for my backups (which appeared to be <code>/volume1/BackupLennart</code> after logging in with SSH).</p>
<p>After making a hole in her firewall that allowed me to connect to the DS211j, I created a backup script in <code>/etc/cron.daily</code> with the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># This script makes a backup of my home dirs to a Synology DiskStation at</span>
<span style="color: #666666; font-style: italic;"># another location. I use LVM for my /home, so I make a snapshot first and</span>
<span style="color: #666666; font-style: italic;"># backup from there.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Time-stamp: &lt;2011-02-06 21:30:14 (lennart)&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">###############################</span>
<span style="color: #666666; font-style: italic;"># Some settings</span>
<span style="color: #666666; font-style: italic;">###############################</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># LVM options</span>
<span style="color: #007800;">VG</span>=raidvg01
<span style="color: #007800;">LV</span>=home
<span style="color: #007800;">MNTDIR</span>=<span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>home_rsync_snapshot<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># rsync options</span>
<span style="color: #007800;">DEST</span>=root<span style="color: #000000; font-weight: bold;">@</span>remote-machine.example.com:<span style="color: #000000; font-weight: bold;">/</span>volume1<span style="color: #000000; font-weight: bold;">/</span>BackupLennart<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #007800;">SRC</span>=<span style="color: #800000;">${MNTDIR}</span><span style="color: #000000; font-weight: bold;">/*</span>
<span style="color: #007800;">OPTIONS</span>=<span style="color: #ff0000;">&quot;-e ssh --delete --progress -azvhHS --numeric-ids --delete-excluded &quot;</span>
<span style="color: #007800;">EXCLUSIONS</span>=<span style="color: #ff0000;">&quot;--exclude lost+found --exclude .thumbnails --exclude .gvfs --exclude .cache --exclude Cache&quot;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">###############################</span>
<span style="color: #666666; font-style: italic;"># The real work</span>
<span style="color: #666666; font-style: italic;">###############################</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Create the LVM snapshot</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$MNTDIR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># If the snapshot directory exists, another backup process may be</span>
    <span style="color: #666666; font-style: italic;"># running</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$MNTDIR</span> already exists! Another backup still running?&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #660033;">-1</span>
<span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #666666; font-style: italic;"># Let's make snapshots</span>
    <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$MNTDIR</span>
    lvcreate <span style="color: #660033;">-L5G</span> <span style="color: #660033;">-s</span> <span style="color: #660033;">-n</span> snap<span style="color: #007800;">$LV</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$VG</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$LV</span>
    <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$VG</span><span style="color: #000000; font-weight: bold;">/</span>snap<span style="color: #007800;">$LV</span> <span style="color: #007800;">$MNTDIR</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Do the actual backup</span>
rsync <span style="color: #007800;">$OPTIONS</span> <span style="color: #007800;">$EXCLUSIONS</span> <span style="color: #007800;">$SRC</span> <span style="color: #007800;">$DEST</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Remove the LVM snapshot</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$MNTDIR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">umount</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$VG</span><span style="color: #000000; font-weight: bold;">/</span>snap<span style="color: #007800;">$LV</span>
    lvremove <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$VG</span><span style="color: #000000; font-weight: bold;">/</span>snap<span style="color: #007800;">$LV</span>
    <span style="color: #c20cb9; font-weight: bold;">rmdir</span> <span style="color: #007800;">$MNTDIR</span>
<span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$MNTDIR</span> does not exist!&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #660033;">-1</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>Let&#8217;s walk through it: in the first section I configure several variables. Since I use LVM on my server, I can use it to make a snapshot of my /home partition. The LVM volume group I use is called &#8216;raidvg01&#8242;. Withing that VG my /home partition resides in a logical volume called &#8216;home&#8217;. The variable <code>MNTDIR</code> is the place where I mount the LVM snapshot of &#8216;home&#8217;.</p>
<p>The rsync options are quite straight forward. Check the rsync man page to find out what they mean. Note that I used the <code>--numeric-ids</code> option because the DS211j doesn&#8217;t have the same users as my server and this way all ownerships will still be correct if I ever need to restore from this backup.</p>
<p>In the section called &#8220;The real work&#8221; I first create the <code>MNTDIR</code> directory. Subsequently I create the LVM snapshot and mount it. After this the rsync backup can be run and finally I unmount the snapshot and remove it, followed by the removal of the <code>MNTDIR</code>.</p>
<p>Since the script is placed in <code>/etc/cron.daily</code> it will be executed every day. Since we use SSH to connect to the remote DS211j I set up SSH key access without a password. <a href="http://www.debian-administration.org/article/SSH_with_authentication_key_instead_of_password">This Debian howto</a> will tell you how to set that up.</p>
<p>The only thing missing in this setup is that the backups are not stored in an encrypted form on the remote NAS, but for now this is good enough. I can&#8217;t wait until the network bandwidth on both sides of this backup connection get so fast (and affordable) that I can easily sync my music as well. Right now uploads are so slow that I hardly dare to include those. I know that I shouldn&#8217;t complain since the Netherlands has one of the highest broadband penetrations in the world, but, hey, don&#8217;t you just always want a little more, just like Oliver Twist?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.karssen.org/2011/02/06/using-rsync-to-backup-to-a-remote-synology-diskstation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Script to tunnel RDP connections through stepping stone server using SSH</title>
		<link>http://blog.karssen.org/2010/02/10/script-to-tunnel-rdp-connections-through-a-stepping-stone-server-using-ssh/</link>
		<comments>http://blog.karssen.org/2010/02/10/script-to-tunnel-rdp-connections-through-a-stepping-stone-server-using-ssh/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 13:12:05 +0000</pubDate>
		<dc:creator>LCK</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://blog.karssen.org/?p=57</guid>
		<description><![CDATA[In order to connect to the servers at work we need to connect through a stepping stone host (via SSH). Since some of the servers are MS Windows machines which can be managed via the Remote Desktop Protocol (RDP), this traffic needs to be tunneled over SSH as well. I wrote the following bash script [...]]]></description>
			<content:encoded><![CDATA[<p>In order to connect to the servers at work we need to connect through a stepping stone host (via SSH). Since some of the servers are MS Windows machines which can be managed via the Remote Desktop Protocol (RDP), this traffic needs to be tunneled over SSH as well.<br />
I wrote the following bash script to automate setting up the tunnel. It sets some default variables and then looks for an available port between 1234 and 1254 (chosen completely arbitrarily) and uses it for the tunnel. Then it uses the <tt>rdesktop</tt> program to start the RDP connection.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># This script makes an ssh tunnel to a stepping stone server</span>
<span style="color: #666666; font-style: italic;"># and uses it to start an rdesktop connection to the machine </span>
<span style="color: #666666; font-style: italic;"># given as the first argument of the script. </span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># (C) L.C. Karssen</span>
<span style="color: #666666; font-style: italic;"># $Id: winremote.sh,v 1.14 2010/02/10 13:03:08 lennart Exp $</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># User-configurable variables</span>
<span style="color: #007800;">ssh_username</span>=your_steppingstone_username_here
<span style="color: #007800;">steppingstone</span>=steppingstone.your_company.com
<span style="color: #007800;">rdesktop_username</span>=your_windows_username_here
<span style="color: #007800;">rdesktop_domain</span>=your_windows_domain_here
<span style="color: #007800;">rdesktop_opts</span>=<span style="color: #ff0000;">&quot;-z -g 1024x768 -a 16&quot;</span>
<span style="color: #007800;">rdesktop_port</span>=<span style="color: #000000;">3389</span> <span style="color: #666666; font-style: italic;"># This is the standard MS rdesktop port</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># For ordinary users it should not be necessary to change anything below this line. </span>
<span style="color: #666666; font-style: italic;"># Some functions:</span>
usage<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
Usage:
    $program [options] rdesktop_hostname 
&nbsp;
Make a remote desktop connection through an SSH tunnel.
&nbsp;
Options: 
    -h, --help                                   print this help message
    -s, --steppingstone steppingstone_hostname   set other stepping stone host
                                                   (default: $steppingstone)
    -t, --timeout sec                            set timeout (default: 1)
    -v, --verbose                                verbose output
     --version                                   print version
&nbsp;
Note that some customisations need to be made in the first few lines of this 
script (e.g. user names and other defaults)
EOF</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">program</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #007800;">$0</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Command line option parsing. Shift all options </span>
<span style="color: #007800;">verbose</span>=
<span style="color: #007800;">timeout</span>=<span style="color: #000000;">1</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">do</span> 
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">in</span>
	<span style="color: #660033;">-v</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #660033;">--verbose</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #660033;">--debug</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> 
	    <span style="color: #007800;">verbose</span>=<span style="color: #c20cb9; font-weight: bold;">true</span>
	    <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #660033;">--version</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
	    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'$Revision: 1.14 $'</span>
	    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
	    <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #660033;">-t</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #660033;">--timeout</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> 
	    <span style="color: #7a0874; font-weight: bold;">shift</span>
	    <span style="color: #007800;">timeout</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
	   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$timeout</span> <span style="color: #660033;">-lt</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	       <span style="color: #007800;">timeout</span>=<span style="color: #000000;">1</span>
	   <span style="color: #000000; font-weight: bold;">fi</span>
	   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	       <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Timeout set to <span style="color: #007800;">$timeout</span>&quot;</span>
	   <span style="color: #000000; font-weight: bold;">fi</span>
	   <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #660033;">--steppingstone</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> 
	   <span style="color: #7a0874; font-weight: bold;">shift</span>
	   <span style="color: #007800;">steppingstone</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
	   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	       <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Steppingstone server is <span style="color: #007800;">$steppingstone</span>&quot;</span>
	   <span style="color: #000000; font-weight: bold;">fi</span>
	   <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #660033;">-h</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #660033;">--help</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> 
	   usage
	   <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
	   <span style="color: #000000; font-weight: bold;">;;</span>
	-<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
	   <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;$0: invalid option $1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
 	   usage
	   <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
	   <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
	   <span style="color: #7a0874; font-weight: bold;">break</span>
	   <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
    <span style="color: #7a0874; font-weight: bold;">shift</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Server name (as seen on the steppingstone) that we want to connect to:</span>
<span style="color: #007800;">rdesktop_server</span>=<span style="color: #007800;">$1</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">################### Config done, let's get to work ########################</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Simple usage description</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$rdesktop_server</span>&quot;</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error: No rdesktop host given&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
    usage
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Find a free port on the local machine that we can use to connect through</span>
<span style="color: #007800;">min_port</span>=<span style="color: #000000;">1234</span>
<span style="color: #007800;">max_port</span>=<span style="color: #000000;">1254</span>
<span style="color: #007800;">used_ports</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">netstat</span> <span style="color: #660033;">-tan</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $4}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> 127.0.0.1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> -g<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Used ports are: <span style="color: #007800;">${used_ports[@]}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># In the next line we first print the $used_ports as an array, but with </span>
<span style="color: #666666; font-style: italic;"># each port on a single line. This is then piped to an awk script that </span>
<span style="color: #666666; font-style: italic;"># puts all the values in an array and subsequently walks through all ports </span>
<span style="color: #666666; font-style: italic;"># from $min_port to $max_port in order to find the first port that is not </span>
<span style="color: #666666; font-style: italic;"># in the array. This port is printed.</span>
<span style="color: #007800;">local_port</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;%i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #800000;">${used_ports[@]}</span> <span style="color: #000000; font-weight: bold;">|</span> \
    <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-v</span> <span style="color: #007800;">minp</span>=<span style="color: #007800;">$min_port</span> <span style="color: #660033;">-v</span> <span style="color: #007800;">maxp</span>=<span style="color: #007800;">$max_port</span> \
    <span style="color: #ff0000;">'{ array[$1]=1 } END { for (i=minp; i&lt;=maxp; i++) { if (i in array) continue; else { print i; break } } }'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$local_port</span>&quot;</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error: No ports free! Exiting...&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Selected port was: <span style="color: #007800;">$local_port</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Create tunnel:</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Creating SSH tunnel...&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #007800;">ssh_opts</span>=<span style="color: #ff0000;">&quot;-f -N -L&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #007800;">$ssh_opts</span> <span style="color: #007800;">$local_port</span>:<span style="color: #007800;">$rdesktop_server</span>:<span style="color: #007800;">$rdesktop_port</span> \
    <span style="color: #007800;">$ssh_username</span><span style="color: #000000; font-weight: bold;">@</span><span style="color: #007800;">$steppingstone</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># Allow the ssh tunnel to be established</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #007800;">$timeout</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Abort if tunnel has not been established</span>
<span style="color: #007800;">pidof_ssh</span>=<span style="color: #000000; font-weight: bold;">`</span>pgrep <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;ssh <span style="color: #007800;">$ssh_opts</span> <span style="color: #007800;">$local_port</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidof_ssh</span>&quot;</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error: Timeout while establishing tunnel&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Exiting...&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">3</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Make rdesktop connection</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Opening Remote desktop connection to <span style="color: #007800;">$rdesktop_server</span>...&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
rdesktop <span style="color: #007800;">$rdesktop_opts</span> <span style="color: #660033;">-u</span> <span style="color: #007800;">$rdesktop_username</span> <span style="color: #660033;">-p</span> - \
    <span style="color: #660033;">-d</span> <span style="color: #007800;">$rdesktop_domain</span> localhost:<span style="color: #007800;">$local_port</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Clean up tunnel</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$verbose</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Cleaning up SSH tunnel with pid <span style="color: #007800;">$pidof_ssh</span> and local port <span style="color: #007800;">$local_port</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #007800;">$pidof_ssh</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.karssen.org/2010/02/10/script-to-tunnel-rdp-connections-through-a-stepping-stone-server-using-ssh/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

