I have a socket connection where the client submits their Windows username. The server then runs 'net user username /domain' to find their expiration date. I am trying to find the string "Password expires" and print that line.
print(line) Gives me nothing. If I use
print(result) I do get the entire results for the command but it is kind of garbled looking. How do I get the line with "Password expires"? And do I need to format this a certain way so the "result" doesn't look messed up?
import subprocess import os from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM import sys PORT_NUMBER = 3555 SIZE = 1024 hostName = gethostbyname( '0.0.0.0' ) mySocket = socket( AF_INET, SOCK_DGRAM ) mySocket.bind( (hostName, PORT_NUMBER) ) print ("Test server listening on port {0}\n".format(PORT_NUMBER)) # Receive no more than 1024 bytes while True: (data,addr) = mySocket.recvfrom(SIZE) try: #print >>sys.stderr, 'Received data from', addr while True: print (data.decode()) batcmd=('net user ' + data.decode() + ' /domain') result = subprocess.check_output(batcmd, shell=True) # print (result) shows details but it is messy looking for line in result: if str(line).startswith("Password expires"): print (line) # if data: # print >>sys.stderr, 'sending data back to the client' # mySocket.sendto({ },addr).format(line) # # TypeError: sendto() argument 1 must be string or buffer, not dict # else: # print >>sys.stderr, 'no more data from', client_address # break break # send result back to client ? #mySocket.send(result.encode('ascii')) sys.exit() finally: print('closing socket') mySocket.close() If I print(result), everything looks like this:
.local.\r\n\r\nUser Name js83838\r\nFull Name John Doe \r\n\Country Code Comment Here. Account active Locked\r\nAccount expires more data like this and this UPDATE
I am trying this and it displays every single line instead of 1. Every line has a b' in front of it and looks aligned properly.
while True: (data,addr) = mySocket.recvfrom(SIZE) #print >>sys.stderr, 'Received data from', addr while True: print (data.decode()) batcmd=('net user ' + data.decode() + ' /domain') result = subprocess.check_output(batcmd, shell=True) # print (result) # shows details but it is messy looking for line in result.splitlines(): if str(line).find("Password expires"): print (line)