tkrebes / nisyscfg-python

NI System Configuration Python API
MIT License
4 stars 0 forks source link

[question] how to change the network settings from a remote target? #37

Closed rscd27p closed 1 year ago

rscd27p commented 2 years ago

Hi tkrebes, thanks for all the work you have put into this. I am writing a simple function to get the network configuration of a remote target. The code I have is:

The target is a cRIO-9049 and I am running the code on a Windows 10 machine. My function looks like this:

def get_network_settings(target_name: str,username: str,password: str):
    nisystem_resource = {}
    print("Trying to connect to target: " + target_name)
    with nisyscfg.Session(target=target_name,username=username,password=password) as session: 
        print("Succesfully connected to target: " + str(session.resource.name))
        print("session.resource dir: " + str(dir(session.resource)) + "\n\n")
        nisystem_resource['serial_number'] = str(session.resource.serial_number)
        nisystem_resource['tcp_ip_v4_address'] = str(session.resource.tcp_ip_v4_address)
        nisystem_resource['tcp_ip_v4_subnet'] = str(session.resource.tcp_ip_v4_subnet)
        nisystem_resource['tcp_ip_v4_gateway'] = str(session.resource.tcp_ip_v4_gateway)
        nisystem_resource['tcp_ip_v4_dns_server'] = str(session.resource.tcp_ip_v4_dns_server)
        print(nisystem_resource)
        print("Closing Session")
        session.close()

It seems that I am having trouble accessing the properties of the hardware resource, I also tried using this format session.resource.get_property()

However, in both cases, I got this error: Status.PROP_DOES_NOT_EXIST

I wanted to read the properties first and after being successful I wanted to try to modify them. Ia m referencing the properties here: Properties.py

Am I doing something wrong? Thanks in advance for your help.

tkrebes commented 1 year ago

Hi,

The session that is opened is a System object. So, the network properties can be a bit confusing. You can access the primary ethernet adaptor properties via the system properties. For example,

def get_network_settings(target_name: str, username: str, password: str):
    nisystem = {}
    print("Trying to connect to target: " + target_name)
    with nisyscfg.Session(target=target_name, username=username, password=password) as session:
        print("Succesfully connected to target: " + str(session.resource.name))
        print("session.resource dir: " + str(dir(session)) + "\n\n")
        nisystem["serial_number"] = str(session.serial_number)
        nisystem["ip_address"] = str(session.ip_address)
        nisystem["subnet_mask"] = str(session.subnet_mask)
        nisystem["gateway"] = str(session.gateway)
        nisystem["dns_server"] = str(session.dns_server)
        print(nisystem)

However, the resource properties of the system do not contain the most of the network properties.

If you want to configure the network adaptors directly, use net interfaces returned by find_hardware(). The following example prints out a report similar to the network settings tab found in NI MAX:

import textwrap
import nisyscfg

def print_network_settings():
    with nisyscfg.Session() as session:
        print("Network Adapters")
        for resource in session.find_hardware():
            if nisyscfg.enums.ServiceType.LOCAL_NET_INTERFACE in resource.service_type:
                print("")
                print(textwrap.dedent(
                f"""
                    {resource.name}{' (Primary)' if resource.is_primary_adapter else ''}
                        Adapter Mode:         {resource.name}
                        MAC Address:          {resource.mac_address}
                        IPv4 Address Mode:    {repr(resource.tcp_ip_request_mode)}
                        IPv4 Address:         {resource.tcp_ip_v4_address}
                        Subnet Mask:          {resource.tcp_ip_v4_subnet}
                        Gateway:              {resource.tcp_ip_v4_gateway}
                        DNS Server:           {resource.tcp_ip_v4_dns_server}
                """).strip() )
tkrebes commented 1 year ago

I assume the question has been answered.