A
A
aloorpro2017-08-26 06:47:34
linux
aloorpro, 2017-08-26 06:47:34

Is it possible in Linux to pass an instance of a class from one process to another and perform actions with it?

Actually, subject. As an example and explanation, I will cite the task that set him before me.
I have a working computer with Linux Mint on it. It runs a Python 3 script (let's call it check.py), which initiates two connections to switches on the network via telnet (done via pexpect), performs actions in each of them, collects information, and then leaves an interactive one in one of the connections, and the second is removed. I wanted to open a second terminal window, which would be interactive with a second connection.
Since I hid all the repetitive actions in classes, the code looked something like this:

from connections import connect_to_switch

switch1 = connect_to_switch('192.168.1.1')
switch2 = connect_to_switch('192.168.2.1')
#Дальше всякие действия с этими соединениями
del switch2
switch1.interact()

That is, I wanted a new terminal window that would have the result of executing switch2.interact (), and I did not want to break the connection to 192.168.2.1. But I didn’t understand at all how this can be done, I don’t quite understand what is needed for such a result, and I didn’t find anything in Google. Therefore, I solved the problem through an additional script:
from connections import connect_to_switch
import subprocess

switch1 = connect_to_switch('192.168.1.1')
switch2 = connect_to_switch('192.168.2.1')
#Дальше всякие действия с этими соединениями
del switch2
subprocess.call("mate-terminal -e '/home/user/script.py {}'".format('192.168.2.1'), shell=True)
switch1.interact()

In the /home/user/script.py script, as you might guess, there are 3 lines, which are given twice already above.
Some considerations that appeared in my head formulated already clarified questions:
  • How can I get the object defined in check.py, and what parameters are needed for this?
  • How can you perform an action on it that is defined only in check.py?
  • Perhaps it is possible to launch an interactive on switch2 in the script itself (parent), and then combine a new terminal window with this session? (if yes, please explain in principle)
  • If the program is basically unable to execute its code within another process, please explain why.
  • On what topics do you need to eliminate your illiteracy in order to be able to answer all these questions yourself?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question