Share a piece of code for parsing VPN Profile for iOS/Android devices using Shell and Python scripts

Source: Internet
Author: User
Tags mkdir openssl openssl x509 pkcs12 websense

If you don't talk nonsense, go directly to the code.

Parsing VPN Profile for iOS devices using Shell and Python scripts

The code is as follows: Copy code
#! /Bin/bash

Httpserver = "http: // 10.226.200.96 /"
Vpnprofilename = "vpn. mobileconfig"


If [! -D "/tmp/vpn"]; then
Mkdir/tmp/vpn
Fi
Cp ../tms_rest/$ vpnprofilename/tmp/vpn
Cp ../tms_rest/get_pwd_plist.py/tmp/vpn
Cd/tmp/vpn

If [! -F $ vpnprofilename]; then
Wget $ httpserver $ vpnprofilename-o log
Fi

# Openssl smime-verify-noverify-nochain-inform DER-in $ vpnprofilename | grep verification
Python get_pwd_plist.py $ vpnprofilename
Sed-I's/^ [\ t] * // g' user. p12
Base64-d user. p12> user. p126
Openssl pkcs12-in user. p126-out user. cert-passin file: passin-nodes
# Openssl pkcs12-in user. p12-out user. cert-passin file: passin-nodes
Openssl x509-in pem-in user. cert-noout-text | tr-d ''| grep" Username: "| sed's/; // \ n/g'
Cat pacfile
Echo''
Cat removeoption
Echo''

# Cat vpnexception

Bytes --------------------------------------------------------------------------------------

Get_pwd_plist.py

Import plistlib
Import sys
From xml. etree import ElementTree

Def save_file (filename, content ):
If filename and content:
Fd = open (filename, 'w ')
Fd. write (content)
Fd. close ()

Class ParsePayloadFile (object ):
Def _ init _ (self ):
# Self. payloadfile_name = 'vpn. Leleconfig'
Self. payloadfile_name = sys. argv [1]
Self. pacfile_name = 'pacfile'
Self. passinfile_name = 'passin'
Self. p12file_name = 'User. P12'
Self. removeoption_name = 'removeoption'
Self. vpnexception_name = 'vpnexception'

Def _ del _ (self ):
Pass

Def get_child_node (self, node, child_node_name ):
Node_childs = node. getchildren ()
For child in node_childs:
If child. text = child_node_name:
Return child
Return None

Def get_next_node (self, node, child_node_name ):
Node_childs = node. getchildren ()
Next_node = False
For child in node_childs:
If next_node:
Return child
Elif child. text = child_node_name:
Next_node = True
Return None

Def parse_payloadfile_xml (self ):
Result = ElementTree. parse (self. payloadfile_name)
Node_root = result. getroot ()
Node_dict = node_root.getchildren () [0]
# Print node_dict
Node_payloads_val = self. get_next_node (node_dict, 'payloadcontent ')
Self. node_vpn = node_payloads_val.getchildren () [0]
Node_pro_val = self. get_next_node (self. node_vpn, 'proxies ')
Node_pro_url_val = self. get_next_node (node_pro_val, 'proxyauto1_lstring ')
Save_file (self. pacfile_name, node_pro_url_val.text)
# Print node_pro_url_val.text

'''Node _ ipsec_val = self. get_next_node (self. node_vpn, 'ipsec ')
Node_ipsec_ondemandrules_val = self. get_next_node (node_ipsec_val, 'ondest-rules ')
Ondemandrules_dict = node_ipsec_ondemandrules_val.getchildren () [3]
Node_ActionParameters_val = self. get_next_node (ondemandrules_dict, 'actionparameters ')
Vpn_exception_list = '-' * 40 + '\ n'
Vpn_exception_list + = 'ondemandrules: \ n'
Dict_Domains = node_ActionParameters_val.getchildren () [0]
Node_domains_val = self. get_next_node (dict_Domains, 'Domains ')
For item in node_domains_val:
Vpn_exception_list + = item. text + '\ n'
Node_domainsaction_val = self. get_next_node (dict_Domains, 'domainaction ')
DomainAction = "DomainAction: % s" % node_domainsaction_val.text
Vpn_exception_list + = DomainAction + '\ n'

Node_ipsec_exception_val = self. get_next_node (node_ipsec_val, 'ondemandmatchdomainsnever ')
Vpn_exception_list + = '-' * 20 + '\ n'
Vpn_exception_list + = 'ondemandmatchdomainsnever: \ n'
For item in node_ipsec_exception_val:
Vpn_exception_list + = item. text + '\ n'
Save_file (self. vpnexception_name, vpn_exception_list )'''

Node_py4 = node_payloads_val.getchildren () [4]
Node_py4_pw = self. get_next_node (node_py4, 'password ')
Save_file (self. passinfile_name, node_py4_pw.text)
# Print node_py4_pw.text

Node_py4_pc = self. get_next_node (node_py4, 'payloadcontent ')
# Print node_py4_pc.text
Save_file (self. p12file_name, node_py4_pc.text)

Node_vpntag = self. get_next_node (node_dict, 'payloadremovaldisallowed ')
Remove_vpn_profile = "PayloadRemovalDisallowed: % s" % node_vpntag.tag
Save_file (self. removeoption_name, remove_vpn_profile)

Def parse_payloadfile (self ):
Fd = open (self. payloadfile_name, 'r ')
# Fd = open (sys. argv [1], "r ")
Text = fd. readlines ()
Fd. close ()

Content =''
For item in text:
If item. find ('<? Xml ')> = 0:
Start_pos = item. find ('<? XML ')
End_pos = item. find ('?> ') + 3
Content = item [start_pos: end_pos]
Continue

If item. find ('</plist>') =-1:
Content = content + item
Else:
Content = content + item
Break
# Save_file('content.txt ', content)
# Print content
If content:
Pl = plistlib. readPlistFromString (content)
If pl:
# Print pl
# Print pl ['payloadremovaldisallowed']
Remove_vpn_profile = "PayloadRemovalDisallowed: % s" % pl ['payloadremovaldisallowed ']
Save_file (self. removeoption_name, remove_vpn_profile)

Pl_content = pl ['payloadcontent']
For item in pl_content:
# Print item
If item. has_key ('proxies '):
Save_file (self. pacfile_name, item ['proxies '] ['proxyautostring'])
If item. has_key ('password '):
Save_file (self. passinfile_name, item ['password'])
# Save_file ('user. B64', item ['payloadcontent']. data)
Save_file (self. p12file_name, item ['payloadcontent']. data)

If _ name _ = '_ main __':
Pp = ParsePayloadFile ()
Pp. parse_payloadfile_xml ()


Parsing VPN Profile for Android devices using Shell and Python scripts

The code is as follows: Copy code
#! /Bin/bash

Httpserver = "http: // 10.226.200.96 /"
Vpnprofilename = "vpn_ard.mobileconfig"


If [! -D "/tmp/vpn"]; then
Mkdir/tmp/vpn
Fi
Cp ../tms_rest/$ vpnprofilename/tmp/vpn
Cp ../tms_rest/get_ard_vpnprofile.py/tmp/vpn
Cd/tmp/vpn


If [! -F $ vpnprofilename]; then
Wget $ httpserver $ vpnprofilename-o log
Fi


Python get_ard_vpnprofile.py $ vpnprofilename
Cat ard_vpn | tr-d ''| grep" Username: "| sed's/; // \ n/g'
Cat pacfile

Echo''

Slave --------------------------------------------------------------------------------------------------------------------------------

Get_ard_vpnprofile.py

Import plistlib
Import sys
From xml. etree import ElementTree


Def save_file (filename, content ):
If filename and content:
Fd = open (filename, 'w ')
Fd. write (content)
Fd. close ()

Class ParsePayloadFile (object ):
Def _ init _ (self ):
# Self. payloadfile_name = 'vpn. Leleconfig'
Self. payloadfile_name = sys. argv [1]
Self. pacfile_name = 'pacfile'
Self. passinfile_name = 'passin'
Self. p12file_name = 'User. P12'
Self. removeoption_name = 'removeoption'
Self. ard_vpnprofile = 'ard _ vpn'

Def _ del _ (self ):
Pass

Def get_child_node (self, node, child_node_name ):
Node_childs = node. getchildren ()
For child in node_childs:
If child. text = child_node_name:
Return child
Return None

Def get_next_node (self, node, child_node_name ):
Node_childs = node. getchildren ()
Next_node = False
For child in node_childs:
If next_node:
Return child
Elif child. text = child_node_name:
Next_node = True
Return None

Def parse_payloadfile_ard (self ):
Et = ElementTree. parse (self. payloadfile_name)
Root = et. getroot ()
Nodes = root. getchildren ()
For node in nodes:
Key = node. get ('key ')
If key = 'com. websense. android. wms. extra. PROXY_PAC_FILE_URL ':
Save_file (self. pacfile_name, node. get ('value '))
Elif key = 'com. websense. android. wms. extra. PROXY_AUTH_STRING ':
Save_file (self. ard_vpnprofile, node. get ('value '))

Def parse_payloadfile_xml (self ):
Result = ElementTree. parse (self. payloadfile_name)
Node_root = result. getroot ()
Node_dict = node_root.getchildren () [0]
# Print node_dict
Node_payloads_val = self. get_next_node (node_dict, 'payloadcontent ')
Self. node_vpn = node_payloads_val.getchildren () [0]
Node_pro_val = self. get_next_node (self. node_vpn, 'proxies ')
Node_pro_url_val = self. get_next_node (node_pro_val, 'proxyauto1_lstring ')
Save_file (self. pacfile_name, node_pro_url_val.text)
# Print node_pro_url_val.text

Node_py4 = node_payloads_val.getchildren () [4]
Node_py4_pw = self. get_next_node (node_py4, 'password ')
Save_file (self. passinfile_name, node_py4_pw.text)
# Print node_py4_pw.text

Node_py4_pc = self. get_next_node (node_py4, 'payloadcontent ')
# Print node_py4_pc.text
Save_file (self. p12file_name, node_py4_pc.text)

Node_vpntag = self. get_next_node (node_dict, 'payloadremovaldisallowed ')
Remove_vpn_profile = "PayloadRemovalDisallowed: % s" % node_vpntag.tag
Save_file (self. removeoption_name, remove_vpn_profile)

Def parse_payloadfile (self ):
Fd = open (self. payloadfile_name, 'r ')
# Fd = open (sys. argv [1], "r ")
Text = fd. readlines ()
Fd. close ()

Content =''
For item in text:
If item. find ('<? Xml ')> = 0:
Start_pos = item. find ('<? XML ')
End_pos = item. find ('?> ') + 3
Content = item [start_pos: end_pos]
Continue

If item. find ('</plist>') =-1:
Content = content + item
Else:
Content = content + item
Break
# Save_file('content.txt ', content)
# Print content
If content:
Pl = plistlib. readPlistFromString (content)
If pl:
# Print pl
# Print pl ['payloadremovaldisallowed']
Remove_vpn_profile = "PayloadRemovalDisallowed: % s" % pl ['payloadremovaldisallowed ']
Save_file (self. removeoption_name, remove_vpn_profile)

Pl_content = pl ['payloadcontent']
For item in pl_content:
# Print item
If item. has_key ('proxies '):
Save_file (self. pacfile_name, item ['proxies '] ['proxyautostring'])
If item. has_key ('password '):
Save_file (self. passinfile_name, item ['password'])
# Save_file ('user. B64', item ['payloadcontent']. data)
Save_file (self. p12file_name, item ['payloadcontent']. data)

If _ name _ = '_ main __':
Pp = ParsePayloadFile ()
Pp. parse_payloadfile_ard ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.