Jump to content
Sign in to follow this  
JerryD

UDF: _DNS_Domain

Recommended Posts

JerryD

I had a small issue with trying to use SMTP functions.

My ISP will not allow any SMTP traffic except through its SMTP server (smtp.isp.com).

At the office, we have a server which can be address as simply smtp, but is really smtp.company.com.

This made it difficult to determine what I could set as the SMTP server. However, if I could determine the Connection-specific DNS Suffix domain (as reported by IPConfig /All), I could just append smtp. to that and be done with it (and it would probably work in most situations too!).

I found that the info was stored in WMI's Win32_NetworkAdapterConfiguration, so wrote this quick and dirty to return the current DNS Domain:

;===============================================================================
; Description:      Returns the "Connection-specific DNS Suffix"
; Parameter(s):     $iFullName - Return full name or just Domain
;                       Default: 0 returns just the domain (e.g. domain.com)
;                       Any other value returns the full domain (e.g. something.domain.com)
; Requirement(s):   None
; Return Value(s):  On Success - String: Connection-specific DNS Domain
;                   On Failure - 0  and Set
;                       @ERROR to:  1 - Error getting WMI Service Object
;                                   2 - No DNS entries found
;                                   3 - Return from query was not an object
; Author(s):        JerryD
; Note(s):
;===============================================================================
Func _DNS_Domain( $iFullName = 0 )
    Local $sRet = '', $colItems = '', $objItem = ''
    Local $objWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2' )
    If @error Then
        SetError( 1, 0, $sRet ) ; Error creating WMI Object
    Else
        $colItems = $objWMIService.ExecQuery ('SELECT * FROM Win32_NetworkAdapterConfiguration' , 'WQL' , 0x10 + 0x20 )
        If IsObj( $colItems ) Then
            For $objItem In $colItems
                If $objItem.DNSDomain Then
                    If $iFullName Then
                        $sRet = $objItem.DNSDomain
                    Else
                        $sRet = StringTrimLeft( $objItem.DNSDomain, StringInStr($objItem.DNSDomain, '.' , 0, -2) )
                    EndIf
                    ExitLoop
                EndIf
            Next
            If Not $sRet Then
                SetError( 2, 0, $sRet )
            EndIf
        Else
            SetError( 3, 0, $sRet ) ; Return from WMI Query was not an Object
        EndIf
    EndIf
    $objWMIService = ''
    $colItems = ''
    $objItem = ''
    Return $sRet
EndFunc  ;==>_DNS_Domain

As indicated, by default the function will return just the domain (domain.com), but if any value is passed, it will return the complete DNS Domain name.

So now when I need to declare the SMTP server in a script, I simply use:

$SMTP_Server = 'smtp.' & _DNS_Domain()

_DNS_Domain.au3

Share this post


Link to post
Share on other sites
Emiel Wieldraaijer

this will work is some situations ..

there are some small problems

- you'll have to set a DNS record SMTP.domain.com

- futher the domainname of the smtp server can be different from your local domain name


Best regards,Emiel Wieldraaijer

Share this post


Link to post
Share on other sites
JerryD

this will work is some situations ..

there are some small problems

- you'll have to set a DNS record SMTP.domain.com

- futher the domainname of the smtp server can be different from your local domain name

Your comments relate to how I'm using the function, not the function itself. My intent was to present a UDF which returns information similar to the @LogonDNSDomain macro in AutoIt which I couldn't use on notebooks I brought home from work because the macro reported back my company's domain instead of my home network's (ISP's) domain.

I have no idea what you mean by I'd have to set a DNS record. As I said in the original post, in both locations the script is going to be used there are servers named SMTP. What I needed to know was which network I was on - my home network or my office network. I didn't want to rely on simply an IP address, and as it turns out retrieving the "Connection-specific DNS Suffix" allowed me to be able to simply use the UDF to acquire the domain I needed to append to SMTP.

When I use the UDF, it returns "isp.com" when I'm at home, and "company.com" when I'm at the office. This allows me to use

$SMTP_Server = "smtp." & _DNS_Domain()
in my script.

I probably should have said

This made it difficult to determine what I could USE as the SMTP server for _INetSmtpMail function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×