python increment ipaddress

后端 未结 8 2147
情话喂你
情话喂你 2020-12-31 09:24

I would like to increment an ip address by a fixed value.

Precisely this is what I am trying to achieve, I have an ip address say, 192.168.0.3 and I wan

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 10:17

    Convert the last part of your IP address into a number, add 1 to it, and call ifconfig.

    I think the approach of incrementing the last bit will not scale well as we span across networks. –OP

    I thought of mentioning that in my original answer, but didn't, for various reasons. These reasons are as follows:

    • I thought it is unlikely you would need to do this, and could not guess why you'd want to.
    • Even if you did need to do this, you could just parse the second-to-last number.
    • This is only valid for those bits where the netmask is 0.
    • You also have to worry about "special" reserved IP ranges, such as 192.168.etc.etc. Also hex doublets with 0 and possibly ff/255 have special meaning. There are different rules in IPv6.

提交回复
热议问题