Search This Blog

Thursday, October 29, 2009 – modify docx OpenOfficeXML – using content controls

This took me a while to work out, but finally I got it to work.

The Open Office XML documents utilised by Microsoft in Office 2007 are actually zip files that package a number of folders and documents. So to edit these files you need to be able access the contents.

Microsoft provides an SDK for this purpose there is a version 1 and a version 2 (in CTP, at the time of this article). – version 1 – version 2

I downloaded version 2.

I built a simple website, added a reference the .net open xml sdk and created the following page and code behind.

Reference to the open xml sdkimage


aspx page


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="createWordDocTest.aspx.vb" Inherits="createWordDocTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click to write to word" />

code behind

Imports System.IO
Imports System.Xml
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Partial Class createWordDocTest
    Inherits System.Web.UI.Page
    Protected Sub ReplaceCustomXML(ByVal fileName As String, ByVal customXML As String)
        Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(fileName, True)
            Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
            mainPart.DeleteParts(Of CustomXmlPart)(mainPart.CustomXmlParts)
            'Add a new customXML part and then add content
            Dim customXmlPart As CustomXmlPart = mainPart.AddNewPart(Of CustomXmlPart)()
            'copy the XML into the new part...
            Using ts As New StreamWriter(customXmlPart.GetStream())
            End Using
        End Using
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim strPath As String = "c:\temp"
        Dim strtemplate As String = strPath & "\" & "inputDoc.docx"
        Dim strNewFile As String = strPath & "\outputDoc.docx"
        If System.IO.File.Exists(strtemplate) = True Then
            If System.IO.File.Exists(strNewFile) = True Then
            End If
            System.IO.File.Copy(strtemplate, strNewFile)
            Dim xmlDoc As New XmlDocument()
            Dim customXML As String = "<root><CustomerName></CustomerName></root>"
            Dim xmlnode As XmlNodeList = xmlDoc.GetElementsByTagName("CustomerName")
            xmlnode(0).InnerText = "What ever you want to insert"
            customXML = xmlDoc.InnerXml.ToString
            ReplaceCustomXML(strNewFile, customXML)
            Dim doc As WordprocessingDocument = WordprocessingDocument.Open(strNewFile, True)
            Dim mainPart As MainDocumentPart = doc.MainDocumentPart
        End If
    End Sub
End Class

OK, there is still work to do. We are going to use a template document to house a content control which we will find and change the contents of, via a binding with some customXML . The mechanism here is that you will place a content control on the document, then creation a reference (or binding) to a custom XML entity. To change the content control content you will change the custom XML programmatically (via the asp page) and this will change the content control due to the binding.

OK, create a blank document called inputDoc.docx. Now in the ribbon go to the developer tab. (If the tab is not visible go here And insert a plain text control.


with the new control inserted…


click on properties in the developer tab…


click on properties in the developer 


With the control properties dialog up, enter CustomerName into the Title and Tag fields. Click OK.

So we have now setup our control. Now we need to create the customXML and the relationship between the customXML and the control. Save the document into your c:\temp folder (or to the folder of your choice (remember you will need to modify the example code above)) and save it as “inputDoc.docx”.

Now this can be done manually by going into the docX package creating files in folders and blah blah blah…. However there is an easier way (and I like easy…)

The Word 2007 Content Control Toolkit, you can get it here

Once you have downloaded and installed, fire up the program, and open your document, you see something like this


Now we are going to create some custom XML part and bind it to the control. In the bind view on the right, click on the create a new custom XML part, and then click on edit view tab. You should see 



Now change this to






Go to the  bind View tab. Click on the CustomerName entry in the tree, Now select and drag it to the left pane (Content Controls) and over the control you want to create a relationship (in our case the tag CustomerName).

With that done, click save and close the program.

OK, thats it. Fire up your web page and click the button. You should hopefully find that a document called outputDoc.docx has been created alongside inputDoc.docx and has the contents of the control changed to new values.

Note: CustomXML is not supported in the office 2003 compatibility pack so if you are expecting to use this method with some office 2003 users then don’t as it will not work. I found this out to my own annoyance after getting it to work!…




Wednesday, October 14, 2009

ASP.Net - Session Detection and Timeout in masterpages (

I have spent some time tracking down and implementing a solution to detect if a session exists for a user and if so wether it has timed out. If either is true a redirection occurs to the homepage.

I found an article on this that I reference here, I have modified it slightly to accommodate master pages but in essence it is the same. I recommend you read this article as it gives a lot of in depth explanation about its function that I won’t do here.

Now in the above article it talks about creating a custom base page class that inherits from the System.Web.UI.Page class that all pages (aspx) inherit from normally. If you use this method you would have to change any existing pages to inherit from this new custom class. I had a few pages and did not really want to have to do this so I modified the above example to utilise the masterpage that I was using throughout my site.

Create a class file in your App_Code folder called BaseMasterPage.

Imports Microsoft.VisualBasic
Public Class BaseMasterPage
    Inherits System.Web.UI.MasterPage
    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
        If Context.Session IsNot Nothing Then
            If Session.IsNewSession Then
                Dim szCookieHeader As String = Request.Headers("Cookie")
                If (szCookieHeader IsNot Nothing) AndAlso (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0) Then
                    'The session appears to have timedout, show message to that effect
                    'The page below sessionTimeout.aspx, display message then redirects using a meta refresh.
                    'redirect straight to front page if not timeout. This will capture if someone is trying to jump into the site
                End If
            End If
        End If
    End Sub
End Class

Open your codebehind masterPage file (e.g. Masterpage.master.vb) and change the following line from

Inherits System.Web.UI.MasterPage


Inherits BaseMasterPage

This means your masterpage is now inheriting from your custom class and not the default.

Create a sessionTimeOut page (does not have to an aspx can be straight html) but include the following in the head section, this will redirect the page after a set amount of time. Remember to also have a link to the page displayed incase the redirection fails for some reason.

<meta http-equiv="refresh" content="5;URL=/default.aspx">

if you are not redirected in 5 seconds please click

<a href="/defaults.aspx">here</a>

And that should be it. Now on any page that references the masterpage you will have a session check. It will check for a session startup event (without an existing session) and redirect to the homepage (or whatever page you decide). And also detect as session timeout and redirect to the homepage via a message.



Sunday, October 11, 2009

NeatUpload – Inline Progress Bar and ClientID

When I started using Neatupload, I wanted the progress bar to be displayed inline. The examples provided showed some JavaScript that enabled this, however in my application the controls displayed in the page were renamed by ASP (i.e. adding th ctl_…. prefixes), this then throughout the JavaScript.

note: Before looking at this you must be aware that to get this working you need to disable Sessionstate on the page. This is done in the first line of the aspx page

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="MultipleFile.aspx.vb" Inherits="MultipleFile" EnableSessionState="false" %>

This may have consequences on your application, please review the NeatUpload manual for more informaiton. In my situation I had to recode my masterpagefile that was looking for session variables, I had to detect whether the session was valid, I include the VB code here for reference.

If Me.Context.Session IsNot Nothing Then
    Session("sPreviousURL") = Request.ServerVariables("HTTP_REFERER")
End If

Below I show the JavaScript code needed to enable the inline progress bar to be displayed. I believe this should work with all instances (i.e. in the demo.aspx, where the control ID remains the same as in the aspx page, and when the control is being renamed by

The hide/show progress bar script works by changing the style on a div tag that surrounds the iFrame in which the progress bar is display.

window.onload = function() {
    var inlineProgressBar = NeatUploadPB.prototype.Bars["<%= inlineProgressBar.ClientID %>"];
    var origDisplay = inlineProgressBar.Display;
    inlineProgressBar.Display = function() {
        var elem = document.getElementById(this.ClientID); = "block";;
    inlineProgressBar.EvalOnClose = "NeatUploadMainWindow.document.getElementById('" + inlineProgressBar.ClientID + "') = \"none\";";

Below I show the full aspx page, including the JavaScript.

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="MultipleFile.aspx.vb" Inherits="MultipleFile" EnableSessionState="false" %>
<%@ Register TagPrefix="Upload" Namespace="Brettle.Web.NeatUpload" Assembly="Brettle.Web.NeatUpload" %>
<asp:Content ID="Home_CPH" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
            window.onload = function() {
            var inlineProgressBar = NeatUploadPB.prototype.Bars["<%= inlineProgressBar.ClientID %>"];
                var origDisplay = inlineProgressBar.Display;
                inlineProgressBar.Display = function() {
                    var elem = document.getElementById(this.ClientID);
           = "block";
                inlineProgressBar.EvalOnClose = "NeatUploadMainWindow.document.getElementById('"
		+ inlineProgressBar.ClientID + "') = \"none\";";
    <asp:UpdatePanel ID="UpdatePanel_Progress" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
           <asp:panel id="MultipleFileUploadform" runat="server"  visible="false">
                <Upload:MultiFile id="multiFileId" runat="server" UseFlashIfAvailable="true" FlashFilterExtensions="*.raw;*.wiff">
                    <input type="button" value="Pick Files..." class="Button" />
                <br />
                <asp:Button id="submitButtonId" runat="server" Text="Submit" class="Button"/>
                <input type="submit" value="Cancel" text="Cancel" class="Button"/>
            <div style="display:none;">
                <Upload:ProgressBar id="inlineProgressBar" runat="server" inline="true" style="width: 100%;" Triggers="submitButtonId"/>
        <asp:label id="ResultMsg" runat="server" Visible="False" ForeColor="#ff0033"></asp:label>


NeatUpload – Simple setup 2 (without GAC)

NeatUpload Manual available here.

In this posting I use NeatUpload without installing to the Global Assembly Cache. I did this as I thought initially that by installing into the GAC would mean I didnot have to add a project reference but apparently that is not the case. So now I copy the dll into the project/website and reference from there.

Etract the current neatupload zip file to any location.

Copy the dll


into your \<applicationRoot>\bin folder.

Now in VS go website\add reference. Browse to the dll and select.

Now copy the folder ….\NeatUpload-<version>\dotnet\app\NeatUpload into the root of your application. i.e. <applicationRoot>\NeatUpload

In the application root web.config the following needs to be added.

IN <configuration><configSections> add
<section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler,Brettle.Web.NeatUpload"
                 allowLocation="true" />

IN <configuration> add

<neatUpload xmlns="" useHttpModule="true" />

The above two entries are really a holder at this stage, please review the manual for further options, but netupload can be configured by modifing this tag <neatUpload >.

IN <system.web><httpModules> add

<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload" />

IN <system.webServer><modules> add

<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload" preCondition="managedHandler"/>

With the above changes I was then able to create an aspx page with the following tags, this would then compile and allow me to add multiple files.

<%@ Register TagPrefix="Upload" Namespace="Brettle.Web.NeatUpload" Assembly="Brettle.Web.NeatUpload" %>

<Upload:MultiFile id="multiFileId" runat="server" UseFlashIfAvailable="true"/>


Thursday, October 08, 2009

Using NeatUpload – simple setup (with GAC)

NeatUpload Manual available here.

I using this to document what I did to get NeatUpload running in my environment. Mainly to ensure that I have a record the next time I want to use this great open source product. A lot of this is in the netupload manual, but I don’t all of it was, either way I am just documenting for future reference and it it helps anyone else out so much the better.

edit:- I made the assumption that when I was adding this to the GAC I would not have to reference the dll within my in VS. However this was a misunderstanding on my part, and even though the dll is in the GAC you will still have to add a reference in your VS application.

First thing download and extract the latest version.

Extract the archive.

Install the …\NeatUpload-1.3.18\dotnet\app\bin\Brettle.Web.NeatUpload.dll into the GAC (Global Assembly Cache). To view the GAC use windows explorer and type %WINDIR%/assembly.  You cannot just copy dlls into this folder….To install the dll use the GAC installation utility GACUtil.exe, I found this here C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64, however it can be in other locations. use search to find.

GACUtil –i “dlllocation\dll”

Once the dll is installed in the GAC (and this will need to be done wherever you want to use the upload , i.e webserver etc…). View the GAC (as described above) and make note of the version number.


You will use this information to change all the references in the aspx pages to define full string names. i.e.


will now become


Now copy the folder ..\NeatUpload-1.3.18\dotnet\app\NeatUpload into the root of your application.

Now change all references in all apsx, ashx pages within the neatupload folder to full strong name references… i.e. in progress.aspx






In the application web.config the following needs to be added.

IN <configuration><configSections> add

       <section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler,Brettle.Web.NeatUpload,Version=1.3.3519.18793,Culture=neutral,PublicKeyToken=C95290D92C5893C8"

                 allowLocation="true" />

IN <configuration> add

<neatUpload xmlns="" useHttpModule="true" />

The above two entries are really a holder at this stage, please review the manual for further options, but netupload can be configured by modifing this tag <neatUpload >.

IN <system.web><httpModules> add

<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload,Version=1.3.3519.18793,Culture=neutral,PublicKeyToken=C95290D92C5893C8" />

IN <system.webServer><modules> add

<add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule,Brettle.Web.NeatUpload,Version=1.3.3519.18793,Culture=neutral,PublicKeyToken=C95290D92C5893C8" preCondition="managedHandler"/>

With the above changes I was then able to create an aspx page with the following tags, this would then compile and allow me to add multiple files.

<%@ Register TagPrefix="Upload" Namespace="Brettle.Web.NeatUpload" Assembly="Brettle.Web.NeatUpload,Version=1.3.3519.18793,Culture=neutral,PublicKeyToken=C95290D92C5893C8" %>

       <Upload:MultiFile id="multiFileId" runat="server" UseFlashIfAvailable="true"/>


Wednesday, October 07, 2009 VB.Net access shared folder on computer not in domain

I had a web server that sat outside of a domain that needed to access a shared folder on a server within a domain. I didnot want to run the application in a domain user account, which would of been easier to setup, so I had to work out how to allow specific code to access the share.

My investigation led onto impersonation by using the winAPI function logonUser and also netUseAdd. Now as far as I can see NetUseAdd would be the more ideal solution but I am not a true developer and I could not find a good example of how to use this within in page. So I went the logonuser route.

Now there are a whole number of different options with logonUser and really you need to to understand the windows security model to fully understand all the options. I include some of the articles I found that helped me tie down what I need to do, here

Before delving into this I must state that I had to use mirrored accounts to get this working. This means that I had to create an identical user account on the separate web server to the account I wanted to use in the domain. (This goes back to my 2nd paragraph that states the netUseAdd would be ideal as this would of allowed me to not have to use impersonation and connect using the domain account and not use a mirrored account. If you know how to use netUseAdd from I love to know :o) ).

Now to get this working in my application I created a class, that contained the majority of the code in the MSDN article above (, the clas allows me to easy call on the code from any file.

File: App_code\UserImpersonation.vb

Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Web.Security
Imports System.Security.Principal
Imports System.Runtime.InteropServices

Public Class UserImpersonation

    Const LOGON32_LOGON_BATCH = 4
    Const LOGON32_LOGON_UNLOCK = 7
    Const LOGON32_PROVIDER_WINNT35 = 1
    Const LOGON32_PROVIDER_WINNT40 = 2
    Const LOGON32_PROVIDER_WINNT50 = 3

    Dim impersonationContext As WindowsImpersonationContext

    Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
                            ByVal lpszDomain As String, _
                            ByVal lpszPassword As String, _
                            ByVal dwLogonType As Integer, _
                            ByVal dwLogonProvider As Integer, _
                            ByRef phToken As IntPtr) As Integer

    Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
                            ByVal ExistingTokenHandle As IntPtr, _
                            ByVal ImpersonationLevel As Integer, _
                            ByRef DuplicateTokenHandle As IntPtr) As Integer

    Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
    Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

    Public Function impersonateUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean
        Return impersonateValidUser(userName, domain, password)
    End Function

    Public Sub undoimpersonateUser()
    End Sub

    Private Function impersonateValidUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean

        Dim tempWindowsIdentity As WindowsIdentity
        Dim token As IntPtr = IntPtr.Zero
        Dim tokenDuplicate As IntPtr = IntPtr.Zero
        impersonateValidUser = False

        If RevertToSelf() Then
            If LogonUserA(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, token) <> 0 Then
                If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                    tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
                    impersonationContext = tempWindowsIdentity.Impersonate()
                    If Not impersonationContext Is Nothing Then
                        impersonateValidUser = True
                    End If
                End If
            End If
        End If
        If Not tokenDuplicate.Equals(IntPtr.Zero) Then
        End If
        If Not token.Equals(IntPtr.Zero) Then
        End If
    End Function

    Private Sub undoImpersonation()
    End Sub

End Class

Now the call from the code behind page

Dim impersonateUser As New UserImpersonation

impersonateUser.impersonateUser("username", "", "Password")

‘Code utilising impersonation, nothing special here just your code, I create folders here on the network share.


Now there is really one main line in the class file

LogonUserA(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, token)

Now this is setup to work in my scenario of a server outside the domain, but it should also work in other scenarios you just have to configure the


Now I don;t specify a domain in the

impersonateUser.impersonateUser("username", "", "Password") call, this is show the account get authenticated locally on the webserver. But in a different scenario you may want to specify a domain.

OK thats it, as I have said I am not a developer (or class myself as one) so I may not have this fully correct, and if so I apologise just hope this gets someone up and running.

Now if only I could get NetUseAdd() working……


Tuesday, August 25, 2009

Cockney Rhyming slang for ATMs

I think this is great.

Apparently ATMs in London are being translated into Cockney rhyming slang… I can see the displays now….

Huckleberry Finn

Sausage and Mash

Microsoft need to do a translation for windows…. lol…


Wednesday, July 22, 2009


Now this guy is awesome, stumbled onto him on youtube…. He has a load of different videos including a live jimi hedrix session at the hard rock cafe, which is amazing. Check him out below (its my favourite video, I think he was 15 (2007!) and at his website and youtube channel.


Youtube channel -

Myspace -

Website -


Tuesday, July 21, 2009

Part 5 – CentOS setting up VNC server

Setting up VNC Server, including firewall configuration.

If you followed my previous posting (Parts 1 –3) you would have selected the options Server and Server GUI in the CentOS installation wizard. In doing this you would have installed VNC server. VNC server provides a remote desktop capability for the server. Although it is installed we need to configure some elements before we can connect to it. I will assume you have read part 3 SSH, I will assume you can connect using putty and SSH as the root user.

To get VNC server up and running the way I want (replicate a desktop environment remotely) we need to do 3 things

  1. setup vncserver
  2. edit the firewall to allow vnc connections.

edit config file /etc/sysconfig/vncservers

The vncserver configuration file is located in the /etc/sysconfig directory. From the SSH terminal session run our favourite editor vi, (see part 2, section proxy environment variables, for more details on the editor and commands), to edit the vncservers file

vi /etc/sysconfig/vncservers

Once inside this file find the following two lines

# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"

Uncomment these lines (remove the #), now we want to change the myusername to the username of the user you want to allow to have access to vnc. In this case I will use root and change the two lines to.

VNCSERVERARGS[2]="-geometry 800x600 -depth 16"

So lets look at these two lines, the first line sets up the user (in this case root) and the second line defines the resolution of the desktop (800x600) and the color depth (16bit). The 2: that appears referencing the user apart from referencing the user the number is helps dictate what tcp port the VNC server listens on. VNC server listens on 5900 + the user user number (each user has a port defined), so in our case the port will be 5902 (5900 + 2).


If you want to set up multiple users you can do that as follows. be aware that the root user will be port 5901 and linuxuser on 5902.

VNCSERVERS="1:root 2:linuxuser"
VNCSERVERARGS[1]="-geometry 1280x960 -depth 16"
VNCSERVERARGS[2]="-geometry 1024x768 -depth 16"

Once you have finished editing your file save and quit vi :wq.

create / edit xstartup scripts and set vncpassword

We now have to assign a password to each vnc session and configure the xstartup scripts for each user.

Login with each user assigned in the vncservers file. Once logged in, run the vncpasswd program and set the password. Below I show a screenshot of my terminal session logged in as root and running the vncpasswd program.

 imageIn addition to setting the password, the vncpasswd program creates a .vnc folder in the users home folder


Once all users have had their passwords assigned we need to create the xstartupfile in each .vnc folder. We do this by logging in as root and restarting the vncserver service.

service vncserver stop
service vncserver start

Now at this stage we should be able to connect to the vncserver (apart from the firewall config), however if you could connect at this time you will notice that the desktop is very basic and does not show the Gnome desktop. The interface you would see is the basic windows handler called X11. You should be able to do most things from here but thats not what i was after, I wanted a Gnome desktop. So there is 1 thing left to do.

Now earlier we created the xstartup scripts but did nothing with them. We need to edit these files.

vi ~/.vnc/xstartup

uncomment the following two lines (remove the #).

# exec /etc/X11/xinit/xinitrc

Save and quit the file, remember to do this for each user.

Restart the vncserver

service vncserver stop
service vncserver start

Ok, we should be good to go in so far as vnc is concerned however we still need to configure the firewall, this is detailed further below. 

edit the firewall to allow vnc connections.

Connected via SSH login as root and edit the following file


This file contains the setup for the firewall. We are going to add one line to this file. find the following line

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

and insert the following line above it.

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5901:5902 -j ACCEPT

the last line in the firewall list should always be the REJECT rule, this is to ensure that if nothing matches a rule in the listing that it will always be rejected.

That should now be it. We need to reboot the server so type

shutdown -r now

(this will reboot the server immediately).

Once you have rebooted the server, fire up your vnc client (viewer) and try and connect to the server. I show a screenshot of the Ultra VNC viewer (on the windows machine)  I use to connect to the servers.


Other parts in this series


Part 4 – CentOS SSH – Secure Shelling

SSH – Secure Shelling

Now being a windows guy my method of remoting to another machine was RDPing to it. This gave me a machine desktop on a remote machine. I wanted something similar for CentOS, I found VNC however in finding that I also found SSH. So before delving into how to configure VNC, I want to go into SSH a little. Then in my VNC part of the series I will use SSH to configure VNC.

So what is SSH, SSH is similar to telnet however SSH is encrypted where as telnet is not. By default SSH is installed and is running on the CentOS box. If you followed my previous postings on installation then this should be the case. SSH on CentOS is looked after by a daemon called sshd.

So what do we need to connect via SSH to the CentOS box. I connect from a windows machine so found a client called Puttytray ( It is based on a client called Putty. There is no install it is just an executable.

Run up the client.


SSH usually runs over port 22. Enter the machine IP you want to connect to, ensure connection type is SSH. Now you can also type in a session name and save it, this will allow you to quickly connect in the future. In the screenshot above I have saved 3 sessions for connecting to 3 different CentOS machines.

Note: when you  first connect to a machine you will be prompted like below, this is a warning just telling you that the servers certificate is not in your cache (this is the same as the windows trusted cache). I click yes here, as i don’t want to be prompted each time I connect to the machine.



Once past the certificate warning you should be shown the above window, prompting for a logon name for this example lets use root.


Once you have entered the user and password, you should be presented with a command prompt. This should look like the prompt we saw in the previous part of this series. It has placed you in the users home directory (~, this is actually the physical folder /home/<username>). The # indicates you are logged in as root, a normal user is presented with a $.

That is pretty much it, what we have now is a remote command line terminal. So in the next part I will use this remote SSH connection to configure VNC.

Other parts in this series


Named Anchors in Windows Live Writer (using plug-in)

This post will show how to use a plug-in (DynamicTemplate) to add functionality to the WYSIWYG interface in Windows Live Writer (WLW).

This functionality is not built into WLW, I was helpfully pointed at a plug-in for WLW. The plug-in is called DynamicTemplate.

and from my initial looks at it it seems pretty powerful to me. I downloaded it and installed (the install in windows was straight forward, so I won’t cover that). Once installed fire up WLW and you should see in the Insert menu a new option labelled Template.


I have used a blog posting of mine as an example, and shown how to create a template with the plug-in for inserting named anchors. The template I have created when selected will prompt for the name you want to call the anchor.

Please view the video below for a run through.

Notes before watching the video are

_selection : is a special variable for DynamicTemplate and will insert whatever is selected in WLW into the template. In the example video I have no selection, but I include it in the template code so you can wrap the named anchor around some existing object in your post (if for some strange reason you want to do that :o) ).


Monday, July 20, 2009

Part 3 - CentOS Proxy Configuration

CentOS Proxy Configuration

It took me a while to figure this out, being used to Windows and having IE generally used as a central repository for proxy settings, I was looking for something similar in CentOS. I am afraid in this case, as far as I can make out, proxy settings are mostly application specific. That said I believe there are locations that are apparently meant to be used for central proxy configuration and programs should try to get the proxy info from them. The central locations are split between GUI programs and command line programs.

So in this post I show how to configure the proxy settings in 4 locations, 2 of which are “apparently” central locations for other programs to use and the other 2 are program specific (as they seem to ignore the central settings). The 4 locations are

  1. Gnome Proxy (used by GUI programs)
  2. Proxy Environment Variables (used by command line programs)
  3. YUM (Software updater and installer)
  4. Firefox (Web browser)
Gnome Proxy setting

The Gnome desktop interface provides proxy configuration settings and apparently a lot of GUI programs will use this settings. This said, I am afraid these settings need to be changed for each user. (I can’t remember where I cam across this information and if it wrong maybe someone can let me know and I will update this.)

Gnome-Desktop proxy settings can be changed from a GUI tool or you can edit the file directly. The image below shows where to find the GUI tool under

System –> Preferences –> Network Proxy

image You can edit the proxy settings here. I have a proxy configuration file available so I have set the automatic proxy config setting, however you can set the proxy manually here as well. The advanced tab allows you to specifically define addresses to ignore the proxy for (i.e. the local subnet).

imageWhen using this GUI tool Gnome stores the proxy config settings in the following folders and files. ~ indicates the users home directory and the . hides the folder or file(see notes below for further explanation)

~/.gconf/system/proxy/%gconf.xml (used for the manual and advanced settings)
~/.gconf/system/http_proxy/%gconf.xml (used for the automatic settings)

I can’t tell you why it is split into two files but it appears to be so.

Proxy Environment Variables

Like the Gnome proxy settings above, the Environment variables are apparently for Command Line programs to provide a central area that these programs can look to for proxy config.

There are a number of proxy environment variables but I will concentrate on just 3 that apply to my network setup. These are

  1. http_proxy
  2. https_proxy
  3. ftp_proxy

I think these are pretty self explanatory.

To set these there is no GUI (that I am aware of), so we have to go to the command line. We need to fire up Terminal this is found in

Applications–> Accessories –> Terminal


The terminal should start in the users home folder ~ (see notes), you should see a prompt like this

[<username>@<machine_name> ~]#


[<username>@<machine_name> ~]$

<username> should be the logged on user
<machine_name> should be the computer name you are on.
The # or the $ is dependent on whether you are logged in as a user or root. # indicates root, $ indicates normal user.

We can set the environment variables at the command prompt by typing


Now the same applies to the other environment variables. However at the moment these environment variables are only valid within this particular Terminal session. If we were to fire up another program or terminal session theses environment variables would not exist in their scope. So to make the environment variables global we need to export them, this is done using the export command. We can export any number of variables by separating them with a space.

export http_proxy https_proxy ftp_proxy

Another issue here is that this is not a permanent change, if we reboot these variables will be lost. So to get around that we need to change a log on script called profile. The script is found here


OK, so how do we edit a file? I am going to give a brief introduction to vi (mainly because I have only just got my head into it) and some basic bash commands.

So in the terminal window change directory to /etc/.

To get to this directory use the following bash commands

CD / (will take you to the root directory)

CD etc (will take you into the /etc directory)

Now we are in this folder, we can use the following command to list the contents..


In the list of files and folders you should see the file profile listed.

OK now type

vi profile

This puts us into the vi editor, a command line text editor. Now I found this a bit weird a first but I am gradually getting the hang of it. The editor opens up in a mode called command mode. It will allow you to scroll up and down, but not allow you to modify text. This is where you have to change modes, in this case we want to go to insert mode. This can be achieved by hitting the escape key and then i or a

<esc> a/i

You should see at the bottom of the terminal window that – INSERT – appears, to indicate you are in INSERT mode. You can now modify the file. To quit out of this mode hit <esc> at anytime (—INSERT –) should disappear from the bottom of the terminal window.

OK so now we need to enter some the commands to set the variables. So somewhere in the profile file enter the following.


export http_proxy https_proxy ftp_proxy

In my case the same proxy handles http, https and ftp. My script therefore sets the http_proxy variable and then sets the others to the http_proxy value. If you have different settings for https and ftp get rid of the $http_proxy and replace with the direct setting (as with the http_proxy above). Once these have been set I then export the variables to make them global.

OK so we need to save the file by issuing the write command. Hit escape and enter :w <enter>

<esc> :w <enter>

The bottom line should change to show something like

"profile" 60L, 1062C written

Now we have saved the file, we need to quit the editor. The quit command is

<esc> :q <enter>

While using the editor you may find you want to quit without saving, although you have made changes. Now if you just issue the command above it will complain that there are unsaved changes. so to quit without saving changes you can force it by typing

<esc> :q! <enter>

In addition you can combine commands, so to quit and save type

<esc> :wq <enter>

You should now reboot.

You can test that the changes have worked by firing up the terminal (as above) and typing the following command

echo $http_proxy

You should get the value you set in the profile file. The same applies to the other variables if you want to check them.

YUM - Software Updater and Add/remove Software - Proxy Setting

There are two locations within CentOS GUI to run software update tools (software updates and software installations). These are

  • Applications –> System Tools –> Software Updater
  • Applications –> Add/Remove Software

image  image

They both run the same underlying command line application YUM (Yellowdog Updater, Modified) but the Software updater allows for installed software (packages) to be updated, where as the Add/Remove Software option will allow you to add and remove software (packages) from defined repositories. (Repositories are locations on the web/network that software packages can be downloaded/updated from, CentOS comes with a set of predefined repositories but you can add custom repositories when needed).

Now the proxy setting for YUM cannot be configured within any of the GUIs, it has to be done within the configuration files. I will quickly go through this, but it will involve using the vi editor, I will not explain the commands here please see the previous section on environment variables for more details on the each command.

The YUM configuration is yum.conf and is found in /etc/


Enter the vi editor

vi /etc/yum.conf

enter insert mode

<esc> i

find the line that says proxy= and change it to your proxy server settings, if your file does not contain this setting just add it in the first clear line in the [main] section.


Quit and save vi.

<esc> :wq

Now fire up either the Software updater or the Add/Remove Software. You should find that both tools are able to connect to the defined repositories. you can install any updates you require.

Firefox – Proxy Settings

Now I talked about GUI programs respecting the Gnome proxy settings and I believe Firefox does. I really only include this here as a FYI (I messed with the settings when I was trying to work out what was going on with all the proxies). It appears that the default for the proxy settings in Firefox is to “Use System Proxy settings” which means it looks for the Gnome settings. So if you make the changes above you should not have to worry about this.


Open up Firefox and select preferences

Edit –> Preferences


In the advanced section, under the network tab, click settings



Once in Connection settings you can change the your proxy settings.


  1. The folder ~ is actually a shortcut referring to the logged on users home folder (you can see why the setting must be configured for each user). The physical user home folder can be located here /home/<username>.
  2. The . on the beginning of the file or folder name is a way to hide the file or folder from normal folder browsing.
Other parts in this series