0

I am just learning basic python for myself - and hoping to make some simple ..and I mean simple scripts to do a few repetitive tasks

One of these is zoning WWN for Cisco switches in a SAN

Normally we would need 2 WWNs of 2 ports (ie a host =20:00:00:00:00:00:00:00 + Storgae box =50:06:01:00:00:00:00:00) Once we have that, we make a name and add these WWN to it

zone name **host_Storage**
Adding member: member pwwn 50:06:01:00:00:00:00:00 (ie Storage)
Adding member: member pwwn 20:00:00:00:00:00:00:00 (ie host)

I am at the start and want to get a input of a WWN and if its either one of the 2 WWN numbers

  • 2000000000000000
  • 20:00:00:00:00:00:00:00

It will always give me the 2nd format,

ie 20:00:00:00:00:00:00:00

I found something that does this here: Inserting a character at regular intervals in a list

But would like to get this working in a script

This works---

s = '10000000c9abcdef'
':'.join(a + b for a, b in zip(*[iter(s)]*2))

'10:00:00:00:c9:ab:cd:ef'

This does not work (when trying to create a type of script) --

wwn = (input('Enter the WWN or q to quit- ')) 
':'.join(a + b for a, b in zip(*[iter(wwn)]*2))
Community
  • 1
  • 1
olearydc
  • 13
  • 3
  • Seems to work fine for me, what error are you getting (if any) and what version of Python are you running (I am assuming 3+ from the input method)? Only potential problem I see is if you enter a number already colon delimited will still insert them. –  Nov 17 '14 at 16:11
  • ```C:\Python27>www.py ```Enter the WWN or q to quit- 10000000c9abcdef ```Traceback (most recent call last): ```File "C:\Python27\www.py", line 3, in wwn = (input('Enter the WWN or q to quit- ')) ```File "", line 1 ```10000000c9abcdef ^ ```SyntaxError: unexpected EOF while parsing – olearydc Nov 17 '14 at 16:39
  • Hi fredtantini -- tried to pste code -- but does not look great above :) – olearydc Nov 17 '14 at 16:45
  • I will try and change again and get it a bit better--one thing to note is I am not using 3+ at present..Cheers again – olearydc Nov 17 '14 at 16:51
  • If you're using python 2.7 then you need to replace `input()` with `raw_input()` as the former is only used in Python 3+, whereas in Python 2+ is tries to eval the input as a variable. –  Nov 17 '14 at 19:14
  • Thanks iChar.. Used the following .. wwn = (raw_input('Enter the WWN or q to quit- ')) ':'.join(a + b for a, b in zip(*[iter(wwn)]*2)) print wwn ============================== And the respoonse was this ============================== C:\Python27>www.py Enter the WWN or q to quit- 10000000c9abcdef 10000000c9abcdef It did not come back with correct formart Im afraid – olearydc Nov 18 '14 at 11:53
  • :)...cheers for the first bit iChar...modified it a bit more and looks to be working..will mess more with it and expand but you got me started..Cheers again...This is the code that is working and its output====================================================================wwn = (raw_input('Enter the WWN or q to quit- ')) wwnadded =':'.join(a + b for a, b in zip(*[iter(wwn)]*2)) print wwnadded====================================================================C:\Python27>www.py Enter the WWN or q to quit- 2000000000000000 20:00:00:00:00:00:00:00================== – olearydc Nov 18 '14 at 12:05

1 Answers1

0

You Can use regex to achieve the same:

import re
wwn="5006010000000000"
':'.join(re.findall('..', wwn))

'50:06:01:00:00:00:00:00'
Aashutosh jha
  • 552
  • 6
  • 8