Skip to content

Instantly share code, notes, and snippets.

@benwei
Last active November 13, 2019 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwei/f6d869f7587a3bb4db1e4d8ea4322c98 to your computer and use it in GitHub Desktop.
Save benwei/f6d869f7587a3bb4db1e4d8ea4322c98 to your computer and use it in GitHub Desktop.
python script convert string to hex as echo -e command, tested with python 2.7 and 3.5
~$ python2.7 str_to_hex_for_echo_cmd.py
echo -e "testecho_\x21#\x24%\x2a\x5c'\x5c\x22\x21~?<>\x3a| 89920."
~$ python3.5 str_to_hex_for_echo_cmd.py
echo -e "testecho_\x21#\x24%\x2a\x5c'\x5c\x22\x21~?<>\x3a| 89920."
~$ echo -e "testecho_\x21#\x24%\x2a\x5c'\x5c\x22\x21~?<>\x3a| 89920."
testecho_!#$%*\'\"!~?<>:| 89920.
sample="""testecho_!#$%*\\'\\"!~?<>:| 89920."""
echo_validates = "#% _~'?><|,."
data = []
for c in sample:
if c.isalpha() or c.isdigit() or c in echo_validates:
data.append(c)
else:
data.append("\\x%0x" % ord(c))
print('echo -e "%s"\n' % "".join(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment