<p>
Recorded using <a href="https://play.google.com/store/apps/details?id=com.digipom.easyvoicerecorder.pro">Easy Voice Recorder Pro</a>
</p>
<p>I wrote a bash script to connect to various different windows servers from my GNU/Linux desktops. I had a few different requirements:</p>
<ul>
<li>I should be able to call it based on hostname.</li>
<li>All windows should be 90% smaller than my screen.</li>
<li>It should map my keyboard.</li>
<li>It should map my local disk.</li>
<li>It should quickly timeout if the port is not available.</li>
</ul>
<p>You can get the full script <a href="https://kenfallon.com/files/rdp.bash" target="_blank">here</a>, but let’s walk through it:</p>
<p>The first line calls bash and then gets the server name from the symlink that is calling the script. The port is set as “3389”, but you can change that if you like.
</p>
<pre>
#!/bin/bash
SERVER=`basename $0`
PORT="3389"
</pre>
<p>The next few lines finds the smallest vertical and horizontal sizes, even if you are running multiple screens. Then it calculates 90% of that to use as the size.</p>
<pre>
h=$(echo "scale=0;(($(xrandr | grep '*+' | sed 's/x/ /g' | awk '{print $1}' | sort -n | head -1 )/100)*90)" | bc)
v=$(echo "scale=0;(($(xrandr | grep '*+' | sed 's/x/ /g' | awk '{print $2}' | sort -n | head -1 )/100)*90)" | bc)
SIZE=${h}x${v}
</pre>
<p>Next we set the default username and password. I have it ask me for my password but I put it in here as an example.</p>
<pre>
PASSWORD='defaultpassword'
USERNAME='administrator'
WORKGROUP='workgroup'
</pre>
<p>In some cases the credentials may be different, so I have a case statement that will cycle through the servers and apply the differences. Depending on your naming schemes you may be able to use regular expressions here to filter out groups of servers. </p>
<pre>
case "${SERVER}" in
  *server*) echo "Server ${SERVER}"
    PASSWORD='work_password'
    USERNAME='administrator'
    WORKGROUP='WORKGROUP'
    ;;
 
  *colo*) echo "Server ${SERVER}"
    PASSWORD='colo_server_password'
    USERNAME='administrator'
    WORKGROUP='COLODOMAIN'
    ;;
     
  some_server ) echo "Server ${SERVER}"
    PASSWORD='some_server_password'
    USERNAME='some_server_password'
    ;;
  *) echo "No match for ${SERVER}, using defaults"
    ;;
esac
</pre>
<p>Next we use an inbuilt bash command to see if a remote port is open and timeout after one second. </p>
<pre>
timeout 1 bash -c "echo >/dev/tcp/${SERVER}/${PORT}"
</pre>
<p>I used to connect to rdp using the program <a href="https://www.rdesktop.org/" target="_blank">rdesktop</a>, but it is now of limited value due to the fact that there are many open bugs that are not getting fixed. Bugs such as <em><a href="https://bugzilla.redhat.com/show_bug.cgi?id=1075697"><b>Bug 1075697</b></a> -<span id="summary_alias_container"> <span id="short_desc_nonedit_display">rdesktop cannot connect to systems using RDP version 6 or newer</span> </span></em> and <em><a href="https://bugzilla.redhat.com/show_bug.cgi?id=1002978"><b>Bug 1002978</b></a> -<span id="summary_alias_container"> <span id="short_desc_nonedit_display">Failed to negotiate protocol, retrying with plain RDP</span> </span></em>. I then switch to using <a href="https://www.freerdp.com/" target="_blank">xfreerdp</a>. This is the client that is behind <a href="https://remmina.sourceforge.net/" target="_blank">remmina</a>.</p>

<p>You can use <em>xfreerdp /kbd-list</em> to get a list of the available keyboard layouts.</p>
<pre>
if [ $? -eq 0 ]; then
  echo "${SERVER}:${PORT} is open"
  xfreerdp /v:${SERVER} /size:${SIZE} /kbd-type:0x00000409 /t:${SERVER} /d:${WORKGROUP} /u:${USERNAME} /p:${PASSWORD} /a:drive,pc,/ /cert-ignore &amp;
else
  echo "${SERVER}:${PORT} is closed"
fi
</pre>

<p>Next you will need to be sure that your host names are available, either in dns or in your <em> /etc/hosts/</em> file. For example:</p>
<pre>10.1.0.1 server1
10.1.0.2 server2
10.1.0.3 server3
10.2.0.1 coloserver1
10.2.0.2 coloserver2
10.2.0.3 coloserver3
192.168.1.1 some_server
</pre>
<p>Edit the script to your liking and then put it into your a directory in your path, possibly <em>/usr/local/bash</em> or <em>~/bin/</em>. You can then make symbolic links to the servers to the bash script, also in a directory in your path, using the command:</p>
<pre>ln -s /usr/local/bash/rdp.bash ~/bin/some_server
chmod +x ~/bin/some_server
</pre>
<p>Which links the global rdp.bash script to your personal symlink, and makes it executable.</p>
<p>All that you need to do then is type the name of the server and a rdp screen should pop up.</p>
<p>In our example:</p>
<pre>$ some_server
</pre>
<p>From there your Windows Server session should pop up.</p>