Advertisement
itxakapi

remote-order.py

Dec 22nd, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. # remote-order.py Copyrigth 2012 Itxaka Serrano Garcia <itxakaserrano@gmail.com>
  2. # licensed under the GPL2
  3. # see the full license at http://www.gnu.org/licenses/gpl-2.0.txt
  4.  
  5. from pastebin import PastebinAPI
  6. from bs4 import BeautifulSoup
  7. import urllib2, os, logging, datetime
  8.  
  9. # logging config
  10. logging.basicConfig(filename='remote-order.log',level=logging.DEBUG, format='%(levelname)s-%(asctime)s-%(message)s')
  11. logging.info("Script started")
  12.  
  13. # variables to configure
  14. developer_key = ""
  15. user_key = ""
  16. user_login = ""
  17. user_password = ""
  18.  
  19.  
  20. try:
  21.         x = PastebinAPI()
  22.  
  23.         # create the user_key. Not sure if we need to create one every X hours or just one works forever.
  24.  
  25.     if user_key == "":
  26.         user_key = x.generate_user_key(developer_key, user_login, user_password)
  27.         print "Your user key is: ", user_key
  28.         sys.exit(0)
  29.     else:
  30.         continue
  31.  
  32.         # get the pastes
  33.         data = x.pastes_by_user(developer_key, user_key,10)
  34.  
  35.         # parse the data to extract
  36.         soup = BeautifulSoup(data)
  37.         pastes = soup.find_all("paste_key")
  38.  
  39.         # this is needed to get the first paste we made, as we are using it for the commands
  40.         pastes = pastes[len(pastes) - 1]
  41.  
  42.         # download the raw pastebin
  43.         url = "http://pastebin.com/raw.php?i=" +  pastes.text
  44.         cmd = urllib2.urlopen(url)
  45.         cmd =  cmd.read()
  46.  
  47.         # execute and store the output to a file
  48.         os.system(cmd + " > output.txt")
  49.  
  50.         with open("output.txt","r") as f:
  51.                 output = f.read()
  52.  
  53.         # we paste the output to a new pastebin, private, that expires in 10 minutes
  54.         x.paste(developer_key, output, paste_name = "Result of command " + cmd, \
  55.         api_user_key = user_key, paste_private = "private", paste_format=None, paste_expire_date = "10M")
  56.  
  57.         # log the finish
  58.         logging.info("Program finished ok")
  59.  
  60. except Exception as e:
  61.         logging.critical(e)
  62. except TypeError as e:
  63.         logging.critical(e)
  64. finally:
  65.         # remove the output.txt file
  66.         os.remove("output.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement