Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Copyright (c) 2008 Alon Swartz <alon@turnkeylinux.org> - all rights reserved
  3. #Modified significantly by Rik Goldman
  4. """
  5. Configure Psiphon Proxy in MySQL
  6.  
  7. """
  8.  
  9. import sys
  10. import time
  11. import getopt
  12. import socket
  13. import fcntl
  14. import struct
  15.  
  16. from executil import ExecError, system
  17.  
  18. DEBIAN_CNF = "/etc/mysql/debian.cnf"
  19.  
  20. class Error(Exception):
  21. pass
  22.  
  23. def escape_chars(s):
  24. """escape special characters: required by nested quotes in query"""
  25. s = s.replace("\\", "\\\\") # \ -> \\
  26. s = s.replace('"', '\\"') # " -> \"
  27. s = s.replace("'", "'\\''") # ' -> '\''
  28. return s
  29.  
  30. #Set Proxy: verbatim excerpt from code.activestate.com/recipes/439094
  31.  
  32. def get_ip_address ( ifname ):
  33. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  34. return socket.inet_ntoa(fcntl.ioctl(
  35. s.fileno(),
  36. 0x8915,
  37. struct.pack('256s', ifname[:15])
  38. )[20:24])
  39. #Set variables for psiphon.proxy
  40. address = "get_ip_address('eth0')"
  41. proxyname = "administrative"
  42. login_url = "/001/"
  43.  
  44. class MySQL:
  45. def __init__(self):
  46. system("mkdir -p /var/run/mysqld")
  47. system("chown mysql:root /var/run/mysqld")
  48.  
  49. self.selfstarted = False
  50. if not self._is_alive():
  51. self._start()
  52. self.selfstarted = True
  53.  
  54. def _is_alive(self):
  55. try:
  56. system('mysqladmin -s ping >/dev/null 2>&1')
  57. except ExecError:
  58. return False
  59.  
  60. return True
  61.  
  62. def _start(self):
  63. system("mysqld --skip-networking >/dev/null 2>&1 &")
  64. for i in range(6):
  65. if self._is_alive():
  66. return
  67.  
  68. time.sleep(1)
  69.  
  70. raise Error("could not start mysqld")
  71.  
  72. def _stop(self):
  73. if self.selfstarted:
  74. system("mysqladmin --defaults-file=%s shutdown" % DEBIAN_CNF)
  75.  
  76. def __del__(self):
  77. self._stop()
  78.  
  79. def execute(self, query):
  80. system("mysql --defaults-file=%s -B -e '%s'" % (DEBIAN_CNF, query))
  81.  
  82. def usage(s=None):
  83. if s:
  84. print >> sys.stderr, "Error:", s
  85. print >> sys.stderr, "Syntax: %s [options]" % sys.argv[0]
  86. print >> sys.stderr, __doc__
  87. sys.exit(1)
  88.  
  89. def main():
  90.  
  91. m = MySQL()
  92.  
  93. #Set Proxy URL
  94. m.execute('UPDATE psiphon.proxy SET hostneme = \"%s\" WHERE name = "administrative";') % (address)
  95. #Correct query without variables: m.execute('update psiphon.proxy set hostname = "192.168.1.198" where name = "administrative";')
  96. #WORKS with Substitutions: m.execute('REPLACE INTO psiphon.proxy (hostname) VALUES (\"%s\") (name="administrative");') % (hostname)
  97. # set password
  98. #m.execute('update mysql.user set Password=PASSWORD(\"%s\") where User=\"%s\"; flush privileges;' % (escape_chars(password), username))
  99.  
  100. # edge case: update DEBIAN_CNF
  101. #if username == "debian-sys-maint":
  102. # old = file(DEBIAN_CNF).read()
  103. # new = re.sub("password = (.*)\n", "password = %s\n" % password, old)
  104. # file(DEBIAN_CNF, "w").write(new)
  105.  
  106.  
  107. if __name__ == "__main__":
  108. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement