Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #coding: utf-8
  3.  
  4.  
  5. #################
  6. ### MODIFY ME ###
  7. #################
  8.  
  9. PORT = 7777
  10. PASS = 'goodpass'
  11. HOST = 'localhost'
  12.  
  13. #################
  14. ### --------- ###
  15. #################
  16. import sys, os
  17.  
  18. try:
  19.     import dialog
  20. except:
  21.     print "python-dialog is missing"
  22.     sys.exit(1)
  23.  
  24. import socket
  25. import time, datetime
  26. from threading import Thread
  27. import string
  28.  
  29. class main:
  30.     def __init__(self):
  31.  
  32.         self.FILE = file('telnet', 'a')
  33.  
  34.  
  35.         self.run = 1
  36.         self.clones = [('Back', 'Back to main menu')]
  37.         try:
  38.             self.start()
  39.         except dialog.error, exc_instance:
  40.             sys.stderr.write("Error:\n\n%s\n" % exc_instance.complete_message())
  41.             self.exit(1)
  42.    
  43.     def start(self):  
  44.         self.d = dialog.Dialog(dialog="dialog")
  45.         self.d.add_persistent_args(["--backtitle", "PyMuhstik"])
  46.         self.s = self.connect()
  47.        
  48.         do_list = Thread(None, self.listing, None)
  49.         do_list.start()
  50.        
  51.         while 1:
  52.             choice = self.main()
  53.             if choice == 'Clones':
  54.                 selected = self.cloneslist()
  55.             elif choice == 'Actions':
  56.                 action = self.actionlist()
  57.             else:
  58.                 self.exit()
  59.  
  60.     def exit(self, n=0):
  61.         self.run = 0
  62.         self.FILE.close()
  63.         self.s.close()
  64.         sys.exit(n)
  65.  
  66.     def connect(self):
  67.         self.d.gauge_start("Creating socket...", title="Connection to Muhstik")
  68.         s = socket.socket()
  69.         self.d.gauge_update(33, "Connecting to %s port:%s" % (HOST, PORT), update_text=1)
  70.         try:
  71.             s.connect((HOST, PORT))
  72.         except socket.error:
  73.             self.d.gauge_stop()
  74.             self.d.msgbox("Can't connect to muhstik, make sure it's currently running.")
  75.             self.exit(1)
  76.         time.sleep(2)
  77.         self.d.gauge_update(90, "Connection successfull, sending authentification", update_text=1)
  78.         s.send("%s\r\n" % PASS)
  79.         time.sleep(0.3)
  80.         self.d.gauge_update(100, "Done.", update_text=1)
  81.         time.sleep(1)
  82.         self.d.gauge_stop()
  83.         return s
  84.  
  85.     def main(self):
  86.         while 1:
  87.             (code, tag) = self.d.menu(
  88.                 "Main menu",
  89.                 width=40,
  90.                 choices=[("Clones", "Availables clones"),
  91.                          ("Actions", ""),
  92.                          ("Quit", ""),]
  93.                 )
  94.             if self.handle_exit_code(code):
  95.                 break
  96.         return tag
  97.  
  98.     def cloneslist(self):
  99.         while 1:
  100.             (code, tag) = self.d.menu(
  101.                 "Clones list",
  102.                 width=60,
  103.                 choices=self.clones
  104.                 )
  105.             if self.handle_exit_code(code):
  106.                 break
  107.             return tag
  108.  
  109.     def listing(self):
  110.         self.s.send('PyMuhstik')
  111.        
  112.         readbuffer = ''
  113.         while self.run:
  114.             readbuffer=readbuffer+self.s.recv(1024)
  115.             temp=string.split(readbuffer, "\n")
  116.             readbuffer=temp.pop( )
  117.  
  118.             for line in temp:
  119.                 line=string.rstrip(line)
  120.                 line=string.split(line)
  121.  
  122.                 self.FILE.write(line)
  123.  
  124.     def handle_exit_code(self, code):
  125.         if code in (self.d.DIALOG_CANCEL, self.d.DIALOG_ESC):
  126.             if code == self.d.DIALOG_ESC:
  127.                 if self.d.yesno('Do you really want to quit ?') == self.d.DIALOG_OK:
  128.                     self.exit(0)
  129.             else:
  130.                 return 0
  131.         else:
  132.             return 1
  133.  
  134. if __name__ == "__main__":
  135.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement