本文整理汇总了Python中pyeapi.connect_to函数的典型用法代码示例。如果您正苦于以下问题:Python connect_to函数的具体用法?Python connect_to怎么用?Python connect_to使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了connect_to函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
parser = argparse.ArgumentParser(description='Add or remove vlan after checking for existence.')
parser.add_argument('--name', help='supply a descriptive name for this vlan which should be different from the id')
parser.add_argument('vlanid', help='VLAN number to create or remove', action='store', type=int)
parser.add_argument(
'--remove',
help='Use this command to remove the vlan from the switch. Only the vlan id is needed',
action='store_true'
)
args=parser.parse_args()
vlanid = args.vlanid
remove = args.remove
name = args.name
#verify existence of vlan in switch
vlan_exists=check_vlan(vlanid)
'''Actions are below. If the action is to remove a vlan, the switch is checked to see if it is configured.
if not, the vlan is removed.
'''
if remove:
if vlan_exists:
cmd='no vlan {}'.format(vlanid)
pynet_sw2 = pyeapi.connect_to('pynet-sw2')
pynet_sw2.config(cmd)
print 'VLAN {} removed'.format(vlanid)
else:
print 'Nothing to do here! VLAN {} does not exist on switch'.format(vlanid)
#if action is not to remove but add a vlan, first check to see if it exists
else:
if vlan_exists:
if name is not None and vlan_exists != name:
cmd=[]
cmd1 = cmd.append('vlan {}'.format(vlanid))
cmd2 = cmd.append('name {}'.format(name))
pynet_sw2 = pyeapi.connect_to('pynet-sw2')
pynet_sw2.config(cmd)
print 'Vlan found on switch but without a name. Name added to vlan.'
else:
print 'Vlan found on switch with name. Nothing to do here'
else:
cmd = []
cmd1 = cmd.append('vlan {}'.format(vlanid))
cmd2 = cmd.append('name {}'.format(name))
pynet_sw2 = pyeapi.connect_to('pynet-sw2')
pynet_sw2.config(cmd)
print 'Vlan NOT found on switch Adding Vlan.'
示例2: main
def main():
'''
grab "show interface" from switch side
'''
conn1 = pyeapi.connect_to("pynet-sw1")
interfaces = conn1.enable("show interfaces")
interfaces = interfaces[0]['result']
interfaces = interfaces['interfaces']
data = {}
for interface, meta in interfaces.items():
interfaceCounters = meta.get('interfaceCounters', {})
data[interface] = (interfaceCounters.get('inOctets'), interfaceCounters.get('outOctets'))
'''
print out formatted data, I don't know how to print it beautifully, so I referred the solution of teacher
'''
print "\n{:20} {:<20} {:<20}".format("Interface:", "inOctets", "outOctets")
for intf, octets in data.items():
print "{:20} {:<20} {:<20}".format(intf, octets[0], octets[1])
示例3: main
def main():
parser = argparse.ArgumentParser()
parser.add_argument("vlan_id", help="vlan_id ",type=int)
parser.add_argument("--remove", help="option to remove the vlan",action="store_true")
args = parser.parse_args()
vlan_id=args.vlan_id
remove=args.remove
data={}
pynet_sw1=pyeapi.connect_to('pynet-sw1')
show_vlan_data=pynet_sw1.enable('show vlan')
result=check_vlan(show_vlan_data,vlan_id)
if remove:
if result:
print 'VLAN exists, we will remove it \n'
command_str = 'no vlan {}'.format(vlan_id)
pynet_sw1.config([command_str])
else:
print 'VLAN doesn\'t exist, thus there is no point to remove it'
else:
if result:
print 'VLAN exists, we will not create it'
else:
print 'Adding VLAN'
command_str = 'vlan {}'.format(vlan_id)
pynet_sw1.config([command_str])
示例4: main
def main():
'''
Main Loop
'''
parser = argparse.ArgumentParser(description='Test of argparse')
parser.add_argument('--add', action="store", dest="add", help="Add a Vlan with color ex: --add 100")
parser.add_argument('--color', action="store", dest="color", help="Sepecify a VLAN description, ex: --color BLUE")
parser.add_argument('--rem', action="store", dest="rem", help="Remove a VLAN, ex: --rem 100")
parser.add_argument('--version', action='version', version='%(prog)s 0.01')
parseresults = parser.parse_args()
if parseresults.add and parseresults.rem:
sys.exit('You should not specify --add and --rem at the same time')
if parseresults.add and not parseresults.color:
sys.exit('When using --add, you also need to specify the --color argument')
try:
sw_conn = pyeapi.connect_to(SWITCH)
except AttributeError:
sys.exit('Unable to connect to the remote switch')
commands = []
if parseresults.add:
if not check_vlan_exist(sw_conn, parseresults.add):
commands = ['vlan '+parseresults.add, 'name '+parseresults.color]
else:
print 'Vlan %s is already configured on %s' % (parseresults.add, SWITCH)
if parseresults.rem:
if check_vlan_exist(sw_conn, parseresults.rem):
commands = ['no vlan '+parseresults.rem]
else:
print 'Vlan %s not found on %s' % (parseresults.rem, SWITCH)
sw_conn.config(commands)
示例5: main
def main():
'''
Ansible module to manipulate an Arista VLAN
'''
module = AnsibleModule(
argument_spec=dict(
switch=dict(required=True),
vlanid=dict(required=True),
name=dict(required=False),
)
)
vlan_id = module.params.get('vlanid')
vlan_name = module.params.get('name')
switch = module.params.get('switch')
eapi_conn = pyeapi.connect_to(switch)
vlans = eapi_conn.enable("show vlan")
vlans = vlans[0]
vlans = vlans['result']
vlans = vlans['vlans']
if vlan_manipulation(vlans, vlan_id, vlan_name) != 0:
configure(eapi_conn, vlan_id, vlan_name)
if vlan_manipulation(vlans, vlan_id, vlan_name) == 1:
module.exit_json(msg="VLAN already exists, setting VLAN name", changed=True)
if vlan_manipulation(vlans, vlan_id, vlan_name) == 2:
module.exit_json(msg="Adding VLAN including vlan_name (if present)", changed=True)
else:
module.exit_json(msg="VLAN already exists, no action required", changed=False)
示例6: main
def main(args=None):
"""Main execution routine for eapish command
Parse the command line options, create a pyeapi connection to each host,
send the commands, print the output from each host.
"""
args = cmd_line_parser(args)
# Build the command
cmds = ' '.join(args.cmd)
# Set the response encoding
encoding = 'text' if args.text else 'json'
# Parse the list of hosts
hosts = args.hosts.split(',')
# For each host send the commands and store in the responses dict:
for host in hosts:
print '\nHost: %s' % host
# Create connection to host
node = pyeapi.connect_to(host)
if node is None:
print 'Error: "%s" connection profile not found' % host
return 2
for cmd in cmds.split(','):
# Run command and print output
run_cmd(node, args.config, cmd, encoding)
return 0
示例7: main
def main():
'''
Simple Ansible module to create an Arista VLAN
'''
module = AnsibleModule(
argument_spec=dict(
arista_sw=dict(required=True),
vlan_id=dict(required=True),
vlan_name=dict(required=False),
)
)
vlan_id = module.params['vlan_id']
vlan_name = module.params.get('vlan_name')
arista_sw = module.params.get('arista_sw')
print arista_sw
eapi_conn = pyeapi.connect_to(arista_sw)
# Check if VLAN already exists
check_vlan = check_vlan_exists(eapi_conn, vlan_id)
if check_vlan:
if vlan_name is not None and check_vlan != vlan_name:
configure_vlan(eapi_conn, vlan_id, vlan_name)
module.exit_json(msg="VLAN already exists, setting VLAN name", changed=True)
else:
module.exit_json(msg="VLAN already exists, no action required", changed=False)
else:
configure_vlan(eapi_conn, vlan_id, vlan_name)
module.exit_json(msg="Adding VLAN including vlan_name (if present)", changed=True)
示例8: main
def main():
'''
Use Arista's eAPI to obtain 'show vlans' from the switch.
'''
pynet_sw2 = pyeapi.connect_to("pynet-sw2")
vlans = pynet_sw2.enable("show vlan")
vlans = pyeapi_result(vlans)
parser = argparse.ArgumentParser(' Valn Manipulation')
parser.add_argument('--name', '--list', nargs='+', action='store', dest='list',
help='Add a Vlan')
parser.add_argument('--remove', action='store', dest='remove',
help='Remove a Vlan')
results = parser.parse_args()
if results.list != None:
cmds = ['vlan ' + results.list[1], 'name ' + results.list[0]]
if not search_vlan(vlans, results.list[1]):
pynet_sw2.config(cmds)
print " vlan {}, name {} was added successfully ".\
format(results.list[1], results.list[0])
else:
print " vlan {} already exist ".format(results.list[1])
if results.remove != None:
cmds = ['no vlan ' + results.remove]
if search_vlan(vlans, results.remove):
pynet_sw2.config(cmds)
print " vlan {} was removed successfully ".format(results.remove)
else:
print " vlan {} is abscent from the Vlan Data base ".format(results.remove)
示例9: main
def main():
# create the node instance
node = pyeapi.connect_to('pynet-sw1')
# go out and grab the "show interfaces" output
show_int = node.enable('show interfaces')
# Output from above returns a list; striping off list
# leaving me a dictionary
show_int = show_int[0]
# I just want the output of the result key from the
# dictionary above
interfaces = show_int['result']
interfaces = interfaces['interfaces']
print "\n{:20} {:<20} {:<20}".format("Interface", "In Octets", "Out Octets")
for i in interfaces.keys():
list_of_interfaces = interfaces.get(i)
int_c = list_of_interfaces.get('interfaceCounters', {})
inOct = int_c.get('inOctets')
ouOct = int_c.get('outOctets')
print "{:20} {:<20} {:<20}".format(i, inOct, ouOct)
示例10: executecommands
def executecommands(commandlist):
"""
Execute comman list parsed in.
"""
arista_connection = pyeapi.connect_to("pynet-sw2")
result = arista_connection.config(commandlist)
print "Result of commmand execution: %r" % result
示例11: main
def main():
"""
Use eapi to retrieve 'show ip route' from your arista switch.
From the returned data structure retrieve the prefixes, the output interfaces, and the next hops
(if available).
Print these routes and associated information to standard out.
"""
pynet_sw1 = pyeapi.connect_to("pynet-sw1")
output = pynet_sw1.enable("show ip route")
# Strip off unneeded information
output = output[0]
output = output['result']['vrfs']['default']
# Get the routes
routes = output['routes']
print "\n{:>15} {:>15} {:>15}".format("prefix", "interface", "next_hop")
filler = "-" * 15
print "{:>15} {:>15} {:>15}".format(filler, filler, filler)
for prefix, attr in routes.items():
intf_nexthop = attr['vias'][0]
interface = intf_nexthop.get('interface', 'N/A')
next_hop = intf_nexthop.get('nexthopAddr', 'N/A')
print "{:>15} {:>15} {:>15}".format(prefix, interface, next_hop)
print
示例12: main
def main():
'''
Use Arista's eAPI to obtain 'show interfaces' from the switch.
'''
eapi_conn = pyeapi.connect_to("pynet-sw3")
interfaces = eapi_conn.enable("show interfaces")
# Strip off outer list
interfaces = interfaces[0]
# Only use result dictionary
interfaces = interfaces["result"]
# Go one more step down to the interfaces
interfaces = interfaces['interfaces']
# From Kirk's code
data_stats = {}
# inOctets/outOctets are fields inside 'interfaceCounters' dict
for interface, int_values in interfaces.items():
int_counters = int_values.get('interfaceCounters', {})
data_stats[interface] = (int_counters.get('inOctets'), int_counters.get('outOctets'))
# Print output data
print "\n{:20} {:<20} {:<20}".format("Interface:", "inOctets", "outOctets")
for intf, octets in data_stats.items():
print "{:20} {:<20} {:<20}".format(intf, octets[0], octets[1])
示例13: main
def main():
module = AnsibleModule(
argument_spec = dict(
arista_sw=dict(required=True),
vlan_id=dict(required=True),
vlan_name=dict(required=False),
),
supports_check_mode=True
)
vlan_id = module.params['vlan_id']
vlan_name = module.params.get('vlan_name')
arista_sw = module.params.get('arista_sw')
eapi_conn = pyeapi.connect_to(arista_sw)
check_vlan = check_vlan_exists(eapi_conn, vlan_id)
if module.check_mode:
# Check if any changes would be made but don't actually make those changes
module.exit_json(changed=
check_if_system_state_would_be_changed(check_vlan, vlan_name))
if check_vlan:
# VLAN name needs to be set
if vlan_name is not None and vlan_name != check_vlan:
configure_vlan(eapi_conn, vlan_id, vlan_name)
module.exit_json(msg="VLAN already exists, VLAN name set.", changed=True)
else:
module.exit_json(msg="VLAN already exists, no action.", changed=False)
else: # Vlan does not exist
configure_vlan(eapi_conn, vlan_id, vlan_name)
module.exit_json(msg="Add VLAN including vlan_name (if present)", changed=True)
示例14: main
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--name", type=str, default="")
parser.add_argument("vlan", type=str)
parser.add_argument("--remove", action="store_true", default=False)
args = parser.parse_args()
print("Checking for VLAN...")
#print(args.name)
#print(args.vlan)
#print(args.remove)
vlan_id = args.vlan
vlan_name = args.name
pynet_sw2 = pyeapi.connect_to('pynet-sw2')
config = get_config(pynet_sw2)
vlan_status = check_vlan(config,vlan_id)
if args.remove == False:
if vlan_status == False:
print("VLAN does not exist...")
create_vlan(pynet_sw2,vlan_id,vlan_name)
print("\nVLAN has been created.")
else:
print("This VLAN is currently in use.")
else:
if vlan_status == False:
print("No VLAN to remove.")
else:
remove_vlan(pynet_sw2,vlan_id,vlan_name)
print("\nVLAN has been removed.")
示例15: main
def main():
parser = argparse.ArgumentParser()
parser.add_argument("vlan_id", help="vlan_id ",type=int)
parser.add_argument("--name", help="vlan_name_option",type = str)
parser.add_argument("--remove", help="option to remove the vlan",action="store_true")
args = parser.parse_args()
vlan_id=args.vlan_id
remove=args.remove
vlan_name=args.name
print vlan_name
data={}
pynet_sw1=pyeapi.connect_to('pynet-sw1')
show_vlan_data=pynet_sw1.enable('show vlan')
my_list=check_vlan(show_vlan_data,vlan_id)
if my_list:
result=True
vlan_name_current=my_list[1]
else:
result=False
vlan_name_current=None
if remove:
if result:
print 'VLAN exists, we will remove it \n'
command_str = 'no vlan {}'.format(vlan_id)
pynet_sw1.config(command_str)
else:
print 'VLAN doesnt exist, thus there is no point to remove it'
else:
if result:
if str(vlan_name) != str(vlan_name_current):
print type(vlan_name),type(vlan_name_current)
print 'VLAN exists Adding VLAN name'
command_str1 = 'vlan {}'.format(vlan_id)
cmd=[command_str1]
command_str2 = 'name {}'.format(vlan_name)
cmd.append(command_str2)
print cmd
pynet_sw1.config(cmd)
else:
print ' VLAN with same ID and name exists'
else:
if vlan_name != None:
print 'VLAN doesnt exist pushing VLAN ID and name'
command_str1 = 'vlan {}'.format(vlan_id)
cmd=[command_str1]
command_str2 = 'name {}'.format(vlan_name)
cmd.append(command_str2)
pynet_sw1.config(cmd)
else:
print 'No VLAN name is passed, pushign VLAN ID'
command_str = 'vlan {}'.format(vlan_id)
pynet_sw1.config([command_str])