写在前面,

1 对于个人公司需要固定ip,而回家需要用到家里的ip,

2对于公司it人员,每台电脑都需要设置ip,,尤其批量的时候,这个作为it的自己知道

3运维人员,可以通过ip测试哪些ip可以用,哪些不可以用

准备

拥戴wmi库通过pip install wmi安装

代码

由于比较时间紧,没写界面,只能黑窗口操作,侯琼会补上

# -*- coding: utf-8 -*-

import os
import random
import re
from time import sleep
from wmi import WMI
 
#随机修改指定ip段的本机ip
class updateIP:
  def __init__(self):
    self.wmiService = WMI()
    #获取到本地有网卡信息
    self.colNicConfigs = self.wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
    #print self.colNicConfigs[0]
  def getAdapter(self):
    flag = 0
    #遍历所有网卡,找到要修改的那个,这里我是用原ip的第一段正则出来的
    for obj in self.colNicConfigs:
      ip = re.findall("10.\d+.\d+.\d+", obj.IPAddress[0])
      if len(ip) > 0:
        return flag
      else:
        flag = flag+1
  def runSet(self):
    adapter = self.colNicConfigs[self.getAdapter()]
    '''
    #检测ip是否在线,不可用,需登录
    while True:
      ip2 = random.choice(['216', '217'])
      ip3 = random.randint(1, 254)
      ip4 = random.randint(1, 254)
      newIP = '10.%s.%s.%s' % (ip2, ip3, ip4)
      if self.pingIP(newIP):
        break
    '''
    #随机选择了ip的第二段
    ip2 = random.choice(['216', '217'])
    ip3 = random.randint(1, 254)  #随机生成第三段和第二段的值
    ip4 = random.randint(1, 254)
    newIP = '10.%s.%s.%s' % (ip2, ip3, ip4)
    arrIPAddresses = [newIP]  #设置新的ip
    arrSubnetMasks = ['255.248.0.0']  #子网掩码
    arrDefaultGateways = ['10.223.255.254'] #网关
    arrGatewayCostMetrics = [1]   #这里要设置成1,代表非自动选择
    arrDNSServers = ['211.137.191.26']   #dns服务器
    #开始执行修改ip、子网掩码、网关
    ipRes = adapter.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)
    if ipRes[0] == 0:
      print u'\ttip:设置IP成功'
      print u'\t当前ip:%s' % newIP
    else:
      if ipRes[0] == 1:
        print u'\ttip:设置IP成功,需要重启计算机!'
      else:
        print u'\ttip:修改IP失败: IP设置发生错误'
        return False
    #开始执行修改dns
    wayRes=adapter.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric=arrGatewayCostMetrics)
    if wayRes[0] == 0:
      print u'\ttip:设置网关成功'
    else:
      print u'\ttip:修改网关失败: 网关设置发生错误'
      return False
    dnsRes = adapter.SetDNSServerSearchOrder(DNSServerSearchOrder=arrDNSServers)
    if dnsRes[0] == 0:
      print u'\ttip:设置DNS成功,等待3秒刷新缓存'
      sleep(3)
      #刷新DNS缓存使DNS生效
      os.system('ipconfig /flushdns')
    else:
      print u'\ttip:修改DNS失败: DNS设置发生错误'
      return False
'''
  //ping某ip看是否可以通
  def pingIP(self, ip):
    res = os.popen('ping -n 2 -w 1 %s' % ip).read() #内容返回到res
    res = res.decode('gbk')
    if u'请求超时' in res:     #注意乱码编码问题
       return False
    else:
      return True
'''
if __name__ == '__main__':
  update = updateIP()
  update.runSet()
  input()

通过python修改本地ip的更多相关文章

  1. 在Win10中手动添加/修改本地IP

    1 前言 好久没动Win10了... 今天需要用Win10做一下实验,手动修改IP,于是写下了这篇文章作为过程记录. 2 概述 Win10里面修改本地IP不是一件特别困难的事,简单来说可以分为两种方式 ...

  2. python获取本地ip地址的方法

    #_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...

  3. 修改 本地 IP 及 正则表达式 test exec match 的区别

    修改 IP 1.打开 "打开网络和共享中心" 2.打开 "更改适配器设置" 3.打开 "本地连接" 属性 4.修改 "协议版本 4 ...

  4. 一个自动修改本地IP地址的BAT

    set /a num1=%random%%%200+1+1  //生成随机数set ip=192.168.1.//ip 主体set ip1=%ip%%num1% //拼接两部分cmd /c netsh ...

  5. [python网络编程]使用scapy修改源IP发送请求

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  6. 本地连接图标消失;修改地址IP地址

    (1)网络连接中没有本地连接,电脑无法进行拨号.无法上网,右键点击“网上连接”选择“属性”,弹出的“网络连接”文件夹中没有本地连接的图标,类似情况处理起来要相对复杂些了,我们逐一判断故障原因,在想办法 ...

  7. python获取公网ip,本地ip及所在国家城市等相关信息收藏

    python获取公网ip的几种方式       from urllib2 import urlopen   my_ip = urlopen('http://ip.42.pl/raw').read() ...

  8. Python 创建本地服务器环境生成二维码

    一. 需求 公司要做一个H5手机端适配页面,因技术问题所以H5是外包的,每次前端给我们源码,我们把源码传到服务器让其他人访问看是否存在bug,这个不是很麻烦吗?有人说,可以让前端在他们的服务器上先托管 ...

  9. Tomcat在局域网中localhost可以访问,但是无法通过本地ip访问,127.0.0.1也无法访问问题的解决方法

    环境:Tomcat6,Windows Server2008 R2, Tomcat使用默认端口8080. 在BO服务器上使用Tomcat6作为WEB服务器,在服务器本地使用http://localhos ...

  10. python局域网alive ip侦听

    python局域网alive ip侦听 作者:vpoet mails:vpoet_sir@163.com 注:写着玩,欢迎copy # -*- coding: cp936 -*- # coding = ...

随机推荐

  1. C++与C语言中struct 与typedef struct 应用区别(摘自csdn mpp_king)

    typedef是类型定义的意思.typedef struct 是为了使用这个结构体方便.具体区别在于:若struct node {}这样来定义结构体的话.在申请node 的变量时,需要这样写,stru ...

  2. ceph 集群 健康状态报 clock skew detected on mon.tg-ceph-mon-2

    1.现象: health: HEALTH_WARN no active mgr mons are allowing insecure global_id reclaim clock skew dete ...

  3. 本地mysql端口3306 一直干不掉这样解决

    第一步:先whereis   mysql(查找到MySQL的一些本地文件) 主要删除这两个   再干掉端口3306  即可

  4. matlab 将某文件夹的内容复制到另一文件下

    %% 清空close all;clear;clc; %% 选择文件路径(复制某文件夹下部分文件夹到其他路径)folder = uigetdir('C:\Desktop','请选择文件夹'); %% i ...

  5. 洛谷 P2330 [SCOI2005]繁忙的都市 题解

    START: 2021-08-05 15:30:20 题目链接: https://www.luogu.com.cn/problem/P2330 题目详情: 城市C是一个非常繁忙的大都市,城市中的道路十 ...

  6. shell typeset 命令使用修改大小写

    typeset的-u选项可以将一个变量的字符变成大写 1 /home/lee#typeset -u var=abc 2 /home/lee#echo $var 3 ABC -l选项将一个变量的字符变成 ...

  7. oracle中的!=与<>和^=

    oracle中的!=与<>和^=!= . <>.^= 三个符号都表示"不等于"的意思,在逻辑上没有本质区别但是要主义的是三个符号在表达"不等于&q ...

  8. centos虚拟机yum update报错Another app is currently holding the yum lock; waiting for it to exit...

    1.运行yum update报错 [root@localhost ~]# yum update已加载插件:fastestmirror, langpacks/var/run/yum.pid 已被锁定,P ...

  9. Linux查看CPU 内存命令

    查看CPU 内存命令:https://www.cnblogs.com/ggjucheng/archive/2013/01/14/2859613.html 查看某一进程内存占用:ps -ef 获取PID ...

  10. 生成uui

    1.安装uuid包 go get github.com/google/uuid 2.目录结构 3.uuid.go package uuid import ( "github.com/goog ...