Skip to content

Commit 5af8c95

Browse files
Filter out non-printable characters read from syseeprom (#17)
Signed-off-by: Rajendra Dendukuri <rajendra.dendukuri@broadcom.com>
1 parent c959371 commit 5af8c95

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/usr/lib/python3/dist-packages/ztp/DecodeSysEeprom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import sys
1919
import os
2020

21-
from ztp.ZTPLib import runCommand
21+
from ztp.ZTPLib import runCommand, printable
2222

2323
class DecodeSysEeprom:
2424

@@ -64,7 +64,7 @@ def __read_sys_eeprom(self, option):
6464
if not rc == 0 or len(cmd_stdout) != 1:
6565
return 'N.A'
6666
else:
67-
return cmd_stdout[0].rstrip()
67+
return printable(cmd_stdout[0].rstrip())
6868

6969
## Global instance of the class
7070
sysEeprom = DecodeSysEeprom()

src/usr/lib/python3/dist-packages/ztp/ZTPLib.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020
import shlex
2121
import subprocess
22+
from curses.ascii import isprint
2223
import ztp.ZTPCfg
2324
import os.path
2425
from ztp.defaults import *
@@ -272,3 +273,17 @@ def systemReboot():
272273
os.system('reboot -y')
273274
else:
274275
os.system('reboot')
276+
277+
def printable(input):
278+
'''!
279+
Filter out non-printable characters from an input string
280+
281+
@param input (str) Input string
282+
283+
@return String with non-printable characters removed
284+
None if invalid input data type
285+
'''
286+
if input is not None and isString(input):
287+
return ''.join(char for char in input if isprint(char))
288+
else:
289+
return None

src/usr/lib/ztp/ztp-profile.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ ztp_config_create()
117117
DEST=${TMP_ZTP_CONFIG_DB_JSON}
118118
fi
119119

120-
PRODUCT_NAME=$(decode-syseeprom -p)
121-
SERIAL_NO=$(decode-syseeprom -s)
120+
PRODUCT_NAME=$(decode-syseeprom -p | tr -dc '[[:print:]]')
121+
SERIAL_NO=$(decode-syseeprom -s | tr -dc '[[:print:]]')
122122
sonic-cfggen -H -k ${HW_KEY} -a "{\"ZTP_INBAND\": \"$(get_feature inband)\", \
123123
\"ZTP_IPV4\": \"$(get_feature ipv4)\", \"ZTP_IPV6\": \"$(get_feature ipv6)\", \
124124
\"PRODUCT_NAME\": \"${PRODUCT_NAME}\", \"SERIAL_NO\": \"${SERIAL_NO}\"}" \

tests/test_ZTPLib.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import stat
2222
import pytest
2323

24-
from ztp.ZTPLib import runCommand, getField, getCfg
24+
from ztp.ZTPLib import runCommand, getField, getCfg, printable
2525
sys.path.append(getCfg('plugins-dir'))
2626

2727
class TestClass(object):
@@ -114,3 +114,11 @@ def test_getField(self):
114114

115115
data = dict({'key': {'subkey':10} })
116116
assert (getField(data, 'key', dict, None).get('subkey') == 10)
117+
118+
def test_misc(self):
119+
assert(printable("Test-/\=$!()*#!_?><,.][{}+String1234567890") == "Test-/\=$!()*#!_?><,.][{}+String1234567890")
120+
assert(printable("Te\u20ACst\u20AC") == "Test")
121+
assert(printable("\u20AC\u20AC") == "")
122+
assert(printable(None) == None)
123+
assert(printable({"k": "v"}) == None)
124+
assert(printable("") == "")

0 commit comments

Comments
 (0)