modify network adapter configuration parameters

#!/usr/bin/env python
# -*- coding: cp936 -*-




# Author: Jack
# Purpose: modify network adapter configuration parameters
# date: March 30,2012


import wmi




class ModifyNetworkAdapterConfiguration:
    def __init__(self, wmi_object):
        self._nnetwork_adapters = wmi_object.Win32_NetworkAdapterConfiguration(IPEnabled = True)
        if len(self._nnetwork_adapters)<1:
            print("there not find network adapter in your pc\n byebye...")
            exit()
        
    def print_configuration_one(self):
        for obj_network_adapter in self._nnetwork_adapters:
            print obj_network_adapter.Index
            print obj_network_adapter.SettingID
            print obj_network_adapter.Description.encode("cp936")
            print obj_network_adapter.IPAddress
            print obj_network_adapter.IPSubnet
            print obj_network_adapter.DefaultIPGateway
            print obj_network_adapter.DNSServerSearchOrder
    
    def print_configuration_two(self):
        index=1
        for obj_network_adapter in self._nnetwork_adapters:
            print(index, obj_network_adapter.Caption)
            index = index + 1;
            
    def run_one(self):
        self.print_configuration_two()
        
        index = input('please enter order number for network adapter configuration to want view: ')
        nnetwork_adapter = self._nnetwork_adapters[index-1]
        
        print('current network adapter configuration parameters: ')
        print('IP Address: ', nnetwork_adapter.IPAddress)
        print('Subnet Mask: ', nnetwork_adapter.IPSubnet)
        print('Gateway: ', nnetwork_adapter.DefaultIPGateway)
        print('DNS: ', nnetwork_adapter.DNSServerSearchOrder)
        
        ip_address = [raw_input('please input a new ip address for change it: ')]
        subnet_mask = [raw_input('please input a new subnet mask for change it: ')]
        gateway = [raw_input('please input a new gateway for change it: ')]
        gatewayCost = [1]
        dns = [raw_input('please input a new dns for change it: ')]
        
        reboot = 0
        
        # config IP address & subnetmask
        resVal = nnetwork_adapter.EnableStatic(IPAddress = ip_address, SubnetMask = subnet_mask)
        if resVal[0] == 0:
            print("config ip and subnet mask is ok")
        elif resVal[0] == 1: # need reboot os
            print("config ip and subnet mask is ok")
            reboot = reboot + 1
        else:
            print("config ip and subnet mask is failed!", resVal[0])
            exit()
            
        # config default gateway   
        resVal = nnetwork_adapter.SetGateways(DefaultIPGateway = gateway, GatewayCostMetric = gatewayCost)
        if resVal[0] == 0:
            print("config gateway is ok")
        elif resVal[0] == 1: # need reboot os
            print("config gateway is ok")
            reboot = reboot + 1
        else:
            print("config gateway is failed!")
            exit()
            
        # config dns   
        resVal = nnetwork_adapter.SetDNSServerSearchOrder(DNSServerSearchOrder = dns)
        if resVal[0] == 0:
            print("config dns is ok")
        elif resVal[0] == 1: # need reboot os
            print("config dns is ok")
            reboot = reboot + 1
        else:
            print("config dns is failed!")
            exit()
            
        if reboot > 0:
            print('please reboot your os')
        else:
            print('modify network adapter configuration parameters: ')
            print('ip:'.join(nnetwork_adapter.IPAddress))
            print('subnetmask:'.join(nnetwork_adapter.IPSubnet))
            print('default gateway:'.join(nnetwork_adapter.DefaultIPGateway))
            print('DNS:'.join(nnetwork_adapter.DNSServerSearchOrder))
           
        print('modify ip is finish!')
        
    def run_two(self):
        self.print_configuration_two()
        
        index = input('please enter order number for network adapter configuration to want view: ')
        nnetwork_adapter = self._nnetwork_adapters[index-1]
        
        reboot = 0
        
        # config network adapter is dhcp mode
        resVal = nnetwork_adapter.EnableDHCP()
        if resVal[0] == 0:
            print('dhcp mode is ok')
        elif resVal[0] == 1:
            print('dhcp mode is ok')
            reboot = reboot + 1
        else:
            print('dhcp mode is failed')
            exit()
            
        ## config network adapter is dns mode
        #resVal = nnetwork_adapter.EnableDNS(, , , , )
        #if resVal[0] == 0:
        #    print('dns mode is ok')
        #elif resVal[0] == 1:
        #    print('dns mode is ok')
        #    reboot = reboot + 1
        #else:
        #    print('dns mode is failed')
        #    exit()
            
        if reboot > 0:
            print('please reboot your os')
        else:
            print('modify network adapter configuration parameters: ')
            print('ip:'.join(nnetwork_adapter.IPAddress))
            print('subnetmask:'.join(nnetwork_adapter.IPSubnet))
            print('default gateway:'.join(nnetwork_adapter.DefaultIPGateway))
            print('DNS:'.join(nnetwork_adapter.DNSServerSearchOrder))
           
        print('modify ip is finish!')
        
if __name__ == '__main__':
    wmiObj = wmi.WMI()
    modify = ModifyNetworkAdapterConfiguration(wmiObj)
    modify.run_one()
        
        
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例程序,它提供了一个使用CAN接口控制电机操作并设置和修改电机配置参数的C API,并通过命令行界面访问电机配置命令。该程序可以运行在Linux操作系统上。 ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define CAN_INTERFACE "can0" // Define motor control API here void start_motor(); void stop_motor(); void set_motor_speed(int speed); // Define motor configuration API here void set_motor_config(char* config_name, char* config_value); char* get_motor_config(char* config_name); int main(int argc, char** argv) { // Initialize CAN interface here // ... // Parse command line arguments here if(argc < 2) { printf("Usage: %s [command] [arguments]\n", argv[0]); exit(1); } // Handle motor control commands here if(strcmp(argv[1], "start") == 0) { start_motor(); } else if(strcmp(argv[1], "stop") == 0) { stop_motor(); } else if(strcmp(argv[1], "speed") == 0) { if(argc < 3) { printf("Usage: %s speed [speed value]\n", argv[0]); exit(1); } int speed = atoi(argv[2]); set_motor_speed(speed); } // Handle motor configuration commands here else if(strcmp(argv[1], "set") == 0) { if(argc < 4) { printf("Usage: %s set [config name] [config value]\n", argv[0]); exit(1); } set_motor_config(argv[2], argv[3]); } else if(strcmp(argv[1], "get") == 0) { if(argc < 3) { printf("Usage: %s get [config name]\n", argv[0]); exit(1); } char* config_value = get_motor_config(argv[2]); printf("%s\n", config_value); free(config_value); } // Display usage information if command is not recognized else { printf("Unrecognized command: %s\n", argv[1]); exit(1); } return 0; } void start_motor() { // Implement motor start command here // ... } void stop_motor() { // Implement motor stop command here // ... } void set_motor_speed(int speed) { // Implement motor speed command here // ... } void set_motor_config(char* config_name, char* config_value) { // Implement motor configuration set command here // ... } char* get_motor_config(char* config_name) { // Implement motor configuration get command here char* config_value = (char*)malloc(256*sizeof(char)); // ... return config_value; } ``` 在此示例程序中,我们定义了一个基本的C API,用于控制电机操作和设置/修改电机配置参数。我们还定义了一个命令行界面,该界面通过解析命令行参数来调用相应的API函数。此外,我们还可以在适当的位置添加CAN接口初始化和其他必要的代码来实现完整的电机控制系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值