0

The problem is that when using a PYQT5 Gui on the server side, I am unable to send commands to remotely controlled devices (Agents on the host) and have the results (information) sent back to the server with a client identification. The clients will not have a gui. I will have to pick and choose which clients get specific commands based on the client identification or session. I am using the Pyqt5 designer and my python code is decoupled from the main application.

The scenario is that I have a pyqt5 network security incident handling framework that starts a server and then listnes for connections. Then I have a python agent(client.py) that connects back to the server. The connection happens and I can currently only send one command (one variable) to the client and have the results transmitted back to the framework. I am not able to send multiple commands to multiple clients based on client id or session.

One of the biggest problems is that I have a working terminal within my server gui and I am not able to transmit the QLineEdit text to the clients based on sessions. If I add in the python input(->) then everything works great however the users would not be able to use the Gui and I want the users to use the Gui.

while I work on the solution my code examples are located below

https://github.com/jinverar/multiconn-client-server

and the code example at the github is based off the following python tutorial

https://realpython.com/python-sockets/#application-client-and-server

The top links that have helped me the most are listed below.

PyQt5: Sending and receiving messages between client and server

https://github.com/jinverar/TCP-ReverseShell

Server-client connection in PyQt

http://www.mranuran.com/blog/2017/08/04/creating-graphical-chat-application-using-pyqt-and-socket/

Some additional links that have helped me get thus far are listed below.

https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html

https://het.as.utexas.edu/HET/Software/PyQt/qabstractsocket.html#setSocketDescriptor

http://www.mranuran.com/blog/2017/08/04/creating-graphical-chat-application-using-pyqt-and-socket/

https://www.zeolearn.com/magazine/getting-started-guis-with-python-pyqt-qthread-class

The following steps have been recommended to move forward and are a possible solution.

1) instead of an advanced GUI build an extremely simply one that has a button -- when you click the button it sends a message to the server -- then click the button more than once maybe sending a counter value for each button click to the server this way you can test continued connection communication.

2) Then on the server do something similar and see if the server can send a message to your client receiving function and in both cases just print you received the message -- this should help you work those kinks out then you can build from there.

the following points are additional guidance.

1) The Server side should be running as a service on the server -- without a GUI and initially just sending output to your normal console window

2) The simple GUI with Client-Communicator you do first make sure you can send multiple messages to the server and it receives them all

3) Then you build a simple server GUI that talks to your Service program (or you can integrate it if you are not sure how to do that) then click the button a few times to make sure you are talking to the client okay

Once you have that going you should have the communicator tested and working then we just plug in the more robust functionality and more complete GUIs

Then 1) Create the Communicator as a Service

2) Add an API aspect to the Communicator

3) Create the GUI to take commands

4) Have the GUI send the information to the Communicator through a call to the API that you created

to do that

1) Implement the Service by pressing a button if it has not been implemented yet

2) Start/Stop the Service via a call

3) Get data from that Service (or in your case send data to and receive data from)

jedimonk
  • 27
  • 1
  • 6
  • Okay first off (MVC-ish It) --- that is to say use pyqt5 only for rendering your client (or server) GUI and use pure python for these kind of back-end communication implementations. Mixing the two elements is going to create coding issues later on if not right up front. – Dennis Jensen Jul 22 '19 at 17:20
  • I have a version of the server(backend) pure python and a client in pure python and still need help. The pure python server is threaded and I can still only send one command. I can't seem to access the pyqt5 Line edit to send multiple commands. I also have a console version that works flawlessly without pyqt5 but I want the users to see a gui now. – jedimonk Jul 22 '19 at 17:36
  • Okay the GUI front-end would be responsible for "sending" the LineEdit text upon update to the API for the back-end the back-end then would take over and if needed send something to the front-end in response which it would then handle submitting to the display accordingly. As for threading with python keep in mind while you can multi-thread you cannot multi-process with python without adding the multiprocessing feature in or handling the multiprocessing outside of python -- see this https://realpython.com/intro-to-python-threading/ – Dennis Jensen Jul 22 '19 at 20:33
  • Is it possible that because my client_command variable that calls the console_lineedit is nested in a function and both my python server and pyqt server are not able to receive commands? When I add in command = input() my python user input is displayed in the console window like cmd.exe or PowerShell window. The command = input() works perfectly. – jedimonk Jul 23 '19 at 02:39
  • Okay this might help but not sure https://realpython.com/python-sockets/ again the methodology should be client-pyqt-gui talks to client-python-communicator which sends/receives message to the server-python-communicator which talk to the server-pyqt-gui (or command line interface) with that said you do not even need the client-pyqt-gui just a bare bones app that simulates it coupled with your client-python-communicator -- once you get it talking to your server then worry about adding on the GUIs as needed -- basically break it down into smaller pieces and get those working – Dennis Jensen Jul 23 '19 at 13:32
  • @DennisJensen I have taken the input and I am reading the link you provided. That link looks like a GREAT link that I missed and it will help me tremendously. The best part of the link is the "multi-connection client and server". One quick question, how can I get the pyqt5 QlineEdit to send commands to the clients? The QlineEdit is in a function called console_commands() and I can post code that is useful to this question however I'm not sure which code blocks are useful. – jedimonk Jul 23 '19 at 13:52
  • Perhaps this will help there https://stackoverflow.com/questions/22531578/make-an-action-when-the-qlineedit-text-is-changed-programmatically --- but basically when the change event for the LineEdit is encountered you call a function that then grabs the contents of lineedit and wraps-it-up (however protocol requires which I believe you are in full control of) and sends it to your your python-client-communicator API function that you have set up to receive such information – Dennis Jensen Jul 23 '19 at 13:56
  • ok sounds good. My QlineEdit works perfectly to do stuff within the Gui and I can print a help menu to a TextEdit and many other things. I can't seem to send the commands to the clients though. Curently I am using returnPressed.connect. ui.console_lineEdit.returnPressed.connect(console_commands) ui.console_lineEdit.returnPressed.connect(ui.console_lineEdit.clear) – jedimonk Jul 23 '19 at 14:08
  • Okay keep in mind that the GUI is separate from you Communicator so the function that catches the change needs to (1) Get the value contained within your LineEdit (2) perhaps erase your LineEdit contents (3) take the data it got and wrap-it-up (this is just whatever you need to do or not do with that information - such as validation and what not - prior to sending to your Client-Communicator API function -- this you should be able to test by proxy since you could put a print statement inside the Com API function that simply prints received message from GUI – Dennis Jensen Jul 23 '19 at 15:08
  • Once you have that working or instead of you can test your Client-Com method that sends and receives data to/from your Server then once you have the Client-Com talking to the Server correctly and talking to the Client-Gui correctly you just connect those final dots and your done --- Remember autonomy is your friend but you need to implement it correctly or it will be your enemy :) – Dennis Jensen Jul 23 '19 at 15:10
  • @DennisJensen I have reviewed the link and the information is great however the tutorial dosn't work. The server starts and listens however the client does not connect. I have pulled out the information that I think should work and it is located here. https://gist.github.com/jinverar/2a6d9faa63511c696d5b5920094edd90 If it was working then I would add it to this stack overflow question. An additional note is that both my current servers accept multiple clients however I am open to testing a new method. I also learned about the selectors library. – jedimonk Jul 23 '19 at 15:11
  • I have edited the question above – jedimonk Jul 24 '19 at 13:24

0 Answers0