Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import time
  2. import socket
  3. import paramiko
  4. import socks
  5.  
  6. SOCKS5_HOST = "nnn.nnn.nnn.nnn"
  7. SOCKS5_PORT = 12345
  8.  
  9. SERVER_IP = "nnn.nnn.nnn.nnn"
  10. SERVER_PORT = 54321
  11. SERVER_ENCODING = "utf-8"
  12.  
  13. USER_ID = "userid"
  14. USER_PASS = "userpassword"
  15.  
  16. SUCCESS_TOKEN = "successfully"
  17.  
  18.  
  19. def read_until(chan, s):
  20. temp = ""
  21. cnt = 0
  22. while cnt < 10:
  23. try:
  24. temp = temp + chan.recv(1200)
  25. if temp.find(s) >= 0:
  26. break
  27. except socket.timeout:
  28. cnt += 1
  29. time.sleep(0.5)
  30.  
  31. if cnt >= 10:
  32. raise socket.timeout(unicode(temp, SERVER_ENCODING))
  33.  
  34. return temp
  35.  
  36.  
  37. class Result:
  38. def __init__(self):
  39. self.log = ''
  40. self.success = False
  41.  
  42. def add_log(self, s):
  43. self.log += s
  44.  
  45.  
  46. class SSH(object):
  47. def __init__(self, host, port, server_encoding):
  48. socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, host, port, False)
  49. paramiko.client.socket.socket = socks.socksocket
  50. self.SERVER_ENCODING = server_encoding
  51.  
  52. def create_connection(self, host, port, username, password):
  53. self._ssh = paramiko.SSHClient()
  54. self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  55. self._ssh.connect(hostname=host, port=port, username=username, password=password)
  56. self.chan = self._ssh.invoke_shell()
  57. self.chan.settimeout(0)
  58.  
  59. # little example to do something with open connection
  60. def change_password(self, oldpass, newpass):
  61. result = Result()
  62.  
  63. s = read_until(self.chan, 'login: ')
  64. result.add_log(self._u(s))
  65. self.chan.send('passwd\n')
  66.  
  67. s = read_until(self.chan, ':')
  68. result.add_log(self._u(s))
  69. self.chan.send('%s\n' % oldpass)
  70.  
  71. s = read_until(self.chan, ':')
  72. result.add_log(self._u(s))
  73. self.chan.send('%s\n' % newpass)
  74.  
  75. s = read_until(self.chan, ':')
  76. result.add_log(self._u(s))
  77. self.chan.send('%s\n' % newpass)
  78.  
  79. s = read_until(self.chan, '.')
  80. ss = self._u(s)
  81. result.add_log(ss)
  82. if ss.find(self._u(SUCCESS_TOKEN)) >= 0:
  83. result.success = True
  84. else:
  85. result.success = False
  86. return result
  87.  
  88. def _u(self, s):
  89. return unicode(s, self.SERVER_ENCODING)
  90.  
  91. def close(self):
  92. self._ssh.close()
  93.  
  94.  
  95. if __name__ == "__main__":
  96. sshch = SSH(SOCKS5_HOST, SOCKS5_PORT, SERVER_ENCODING)
  97. sshch.create_connection(SERVER_IP, SERVER_PORT,
  98. username=USER_ID, password=USER_PASS)
  99. new_pass = raw_input("New password: ")
  100. result = sshch.change_password(USER_PASS, new_pass)
  101. sshch.close()
  102. if not result.success:
  103. print result.log
  104. raise SystemExit("Error: Changing password failed")
  105. else:
  106. print "Password changed successfully"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement