Reputation: 23
This is not the full code, only snippets of which I'm trying to get to work.
I should warn, I'm still in the learning phases of Python, so if you see anything funky, that's why.
I'm using Tkinter to design a GUI and I want to have a single button press start a handful of commands all at once.
To elaborate on what this program does, it starts an iperf client and then captures telnet readings at the same time. I had a great proof of concept in bash working, but with tkinter I can only seem to get one to start right after the first one has already finished. Using the lambda method below the compiler still complains:
TypeError: () takes exactly 1 argument (0 given)
self.iperfr = Button(frame, text="---Run---",command = lambda x:self.get_info() & self.telnetcap())
self.iperfr.pack(side=BOTTOM)
def get_info():
iperf= self.iperfc.get()
time = self.timeiperf.get()
iperfcommand= 'iperf -c 127.0.0.1 -y c -i 1 {}'.format(iperf)+ ' -t {}'.format(time)
print iperfcommand
os.system(iperfcommand)
def telnetcap():
n=0
time = self.timeiperf.get()
child=pexpect.spawn('telnet 192.168.2.1');
child.logfile = open("/home/alex/Desktop/Test", "w")
child.expect('Login:');
child.sendline('telnet');
child.expect('Password:');
child.sendline('password');
while (n<time):
child.expect('>');
child.sendline('sh');
child.expect('#') ;
child.sendline ('sysinfo');
child.expect ('#');
child.sendline ('iostat');
child.expect ('#');
child.sendline ('exit');
n=n+1
print n
At this point I feel like it might be easier to actually just call my original bash script from within this Python GUI. It seems so trivial, but I'm pulling my hair out trying to get it to work. The simple "&" in bash did exactly what I wanted it to do. Is there a Python version of this?
Thanks!
Upvotes: 0
Views: 2750
Reputation: 23
Thanks Martineau for pointing me in the right direction. Threading was definitely the way to go. By assigning my "run_all" button to a function designated to start individual threads it works great.
def run_all(self):
thread.start_new_thread(self.telnetcap, ())
thread.start_new_thread(self.get_info, ())
Upvotes: 1