ACI安装python SDK

安装ACI SDK之前需要准备

Python 2.7,easy_install,pip,virtualenv,SDK包

virtualenv这个玩意不是必须的,但是ACI版本一直在升级,不同版本的SDK不一样,这个比较烦

SDK包直接在APIC上下载,

https://x.x.x.x/cobra/_downloads/ 

包括类似acicobra-1.1_1j-py2.7.egg, acimodel-1.1_1j-py2.7.egg这两个文件,我这里是1.3.2

我这里virtualenv在D:\python\aci\这个目录下面 通过dir可以看到文件

D:\python\aci\Scripts>dir
2017/03/05  16:11    <DIR>          .
2017/03/05  16:11    <DIR>          ..
2017/03/05  16:02            90,360 acicobra-1.3_2h-py2.7.egg
2017/03/05  16:08        69,542,066 acimodel-1.3_2h-py2.7.egg
2017/03/05  15:30             2,283 activate
2017/03/05  15:30               543 activate.bat
2017/03/05  15:30             8,325 activate.ps1
2017/03/05  15:30             1,137 activate_this.py
2017/03/05  15:30               348 deactivate.bat
2017/03/05  15:30            92,018 easy_install-2.7.exe
2017/03/05  15:30            92,018 easy_install.exe
2017/03/05  15:30            91,990 pip.exe
2017/03/05  15:30            91,990 pip2.7.exe
2017/03/05  15:30            91,990 pip2.exe
2017/03/05  15:30            27,136 python.exe
2017/03/05  15:30            27,648 pythonw.exe
2017/03/05  15:30            91,997 wheel.exe

使用easy_install acicobra-1.3_2h-py2.7.egg命令进行安装  使用easy_install  acimodel-1.3_2h-py2.7.egg安装另外一个包

D:\python\aci\Scripts>easy_install acicobra-1.3_2h-py2.7.egg
Processing acicobra-1.3_2h-py2.7.egg
creating d:\python\aci\lib\site-packages\acicobra-1.3_2h-py2.7.egg
Extracting acicobra-1.3_2h-py2.7.egg to d:\python\aci\lib\site-packages
Adding acicobra 1.3-2h to easy-install.pth file
Installed d:\python\aci\lib\site-packages\acicobra-1.3_2h-py2.7.egg
Processing dependencies for acicobra===1.3-2h
Searching for requests
Reading https://pypi.python.org/simple/requests/
Best match: requests 2.13.0
Downloading https://pypi.python.org/packages/16/09/37b69de7c924d318e51ece1c4ceb6
79bf93be9d05973bb30c35babd596e2/requests-2.13.0.tar.gz#md5=921ec6b48f2ddafc8bb61
60957baf444
Processing requests-2.13.0.tar.gz
Writing c:\users\hs\appdata\local\temp\easy_install-si1rs1\requests-2.13.0\setup
.cfg
Running requests-2.13.0\setup.py -q bdist_egg --dist-dir c:\users\hs\appdata\loc
al\temp\easy_install-si1rs1\requests-2.13.0\egg-dist-tmp-ue8g6c
warning: no files found matching 'test_requests.py'
warning: no files found matching 'requirements.txt'
creating d:\python\aci\lib\site-packages\requests-2.13.0-py2.7.egg
Extracting requests-2.13.0-py2.7.egg to d:\python\aci\lib\site-packages
Adding requests 2.13.0 to easy-install.pth file
Installed d:\python\aci\lib\site-packages\requests-2.13.0-py2.7.egg
Finished processing dependencies for acicobra===1.3-2h

通过pip freeze和pip show acimodel 查看是否安装成功

D:\python\aci\Scripts>pip freeze
acicobra===1.3-2h
acimodel===1.3-2h
requests==2.13.0
wheel==0.24.0
D:\python\aci\Scripts>pip show acimodel
Metadata-Version: 1.1
Name: acimodel
Version: 1.3-2h
Summary: The Management Information Tree
Home-page: UNKNOWN
Author: Cisco Systems
Author-email: mtimm@cisco.com
License: Cisco Systems Inc. (Copyright 2014)
Location: d:\python\aci\lib\site-packages\acimodel-1.3_2h-py2.7.egg
Requires: acicobra

最后在pycharm里面查看

aci-python.png

python 中__name__ = ‘__main__’ 的作用(转)

有句话经典的概括了这段代码的意义:

“Make a script both importable and executable”

意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行

这句话,可能一开始听的还不是很懂。下面举例说明:

先写一个模块:

#module.py
def main():
  print "we are in %s"%__name__
if __name__ == '__main__':
  main()

这个函数定义了一个main函数,我们执行一下该py文件发现结果是打印出”we are in __main__“,说明我们的if语句中的内容被执行了,调用了main():

但是如果我们从另我一个模块导入该模块,并调用一次main()函数会是怎样的结果呢?

#anothermodle.py
from module import main
main()

但是没有显示”we are in __main__“,也就是说模块__name__ = '__main__' 下面的函数没有执行。其执行的结果是:we are in module

这样既可以让“模块”文件运行,也可以被其他模块引入,而且不会执行函数2次。这才是关键。

总结一下:

如果我们是直接执行某个.py文件的时候,该文件中那么”__name__ == '__main__'“是True,但是我们如果从另外一个.py文件通过import导入该文件的时候,这时__name__的值就是我们这个py文件的名字而不是__main__。

这个功能还有一个用处:调试代码的时候,在”if __name__ == '__main__'“中加入一些我们的调试代码,我们可以让外部模块调用的时候不执行我们的调试代码,但是如果我们想排查问题的时候,直接执行该模块文件,调试代码能够正常运行!

linux上临时添加路由和永久添加路由

添加临时路由

[root@fuck ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.18.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.18.2    0.0.0.0         UG    0      0        0 eth0
[root@fuck ~]# route add -host 8.8.8.8 gw 192.168.11.1
[root@fuck ~]# route add -host 114.114.114.114 gw 192.168.18.2
[root@fuck ~]# route add -net 172.16.0.0/16 gw 192.168.18.2
[root@fuck ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
114.114.114.114 192.168.18.2    255.255.255.255 UGH   0      0        0 eth0
8.8.8.8         192.168.11.1    255.255.255.255 UGH   0      0        0 eth1
192.168.18.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
172.16.0.0      192.168.18.2    255.255.0.0     UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.18.2    0.0.0.0         UG    0      0        0 eth0

删除临时路由

[root@fuck ~]# route del -host 8.8.8.8
[root@fuck ~]# route del -host 114.114.114.114
[root@fuck ~]# route del -net 172.16.0.0/16
[root@fuck ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.18.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.18.2    0.0.0.0         UG    0      0        0 eth0

如果需求永久添加路由,就需要增加配置文件,最后的eth0代表对应网卡

vim /etc/sysconfig/network-scripts/route-eth0

添加如下信息

114.114.114.114/32 via 192.168.1.1
8.8.8.8/32 via 192.168.1.1

保存 重启network

 

redhat 7 eth 网卡命名规则

其实大家都知道传统的命名方式有问题,这个问题不仅仅是在linux上有,在VMware这种上也有,之前经常看着搞系统的人给ESXI主机加网卡,加完了就各种不同了,原有都是VINC0之类,加完网卡后就各种混乱了,导致原有的规划和实际不符合,虽然通过重组或重新排序可以解决,但是在实际生产环境是不方便操作的。

redhat也意识到这个问题,在redhat 7里面就重新设定了命令规则,命令规则和网卡插槽位置相关这种是比较科学比较贴近人类的。在过去运维中,系统的网卡还要跟实际网卡插槽或者板载网卡需要维护一个对应关系表,如果设备硬件完全一致还可以记下来,如果设备硬件有太多型号,每次都要去查表才知道系统里面的eth0对应外面那个接口。

这个功能在路由器上其实一直都是实现的很好,是几槽几卡的什么类型接口,配置就可以看出来,例如interface TenG 5/1/1就知道是五槽第一块卡的第一个接口,万兆接口。

以下来自红帽官网:

     Red Hat Enterprise Linux 7 provides methods for consistent and predictable network device naming for network interfaces. These features change the name of network interfaces on a system in order to make locating and differentiating the interfaces easier.

    Traditionally, network interfaces in Linux are enumerated as eth[0123…], but these names do not necessarily correspond to actual labels on the chassis. Modern server platforms with multiple network adapters can encounter non-deterministic and counter-intuitive naming of these interfaces. This affects both network adapters embedded on the motherboard (Lan-on-Motherboard, or LOM) and add-in (single and multiport) adapters.

    In Red Hat Enterprise Linux 7, udev supports a number of different naming schemes. The default is to assign fixed names based on firmware, topology, and location information. This has the advantage that the names are fully automatic, fully predictable, that they stay fixed even if hardware is added or removed (no re-enumeration takes place), and that broken hardware can be replaced seamlessly. The disadvantage is that they are sometimes harder to read than the eth0 or wlan0 names traditionally used. For example: enp5s0.

以下是新的命名规则

By default, systemd will name interfaces using the following policy to apply the supported naming schemes:

  • Scheme 1: Names incorporating Firmware or BIOS provided index numbers for on-board devices (example: eno1), are applied if that information from the firmware or BIOS is applicable and available, else falling back to scheme 2.

  • Scheme 2: Names incorporating Firmware or BIOS provided PCI Express hotplug slot index numbers (example: ens1) are applied if that information from the firmware or BIOS is applicable and available, else falling back to scheme 3.

  • Scheme 3: Names incorporating physical location of the connector of the hardware (example: enp2s0), are applied if applicable, else falling directly back to scheme 5 in all other cases.

  • Scheme 4: Names incorporating interface's MAC address (example: enx78e7d1ea46da), is not used by default, but is available if the user chooses.

  • Scheme 5: The traditional unpredictable kernel naming scheme, is used if all other methods fail (example: eth0).

This policy, the procedure outlined above, is the default. If the system has biosdevname enabled, it will be used. Note that enabling biosdevname requires passing biosdevname=1 as a command-line parameter except in the case of a Dell system, where biosdevname will be used by default as long as it is installed. If the user has added udev rules which change the name of the kernel devices, those rules will take precedence.