简体   繁体   中英

Python - Connect to a remote host from tkinter Script

I am writing a tkinter script which accepts a linux command from the UI. Once the submit button is clicked,I want to run this command that I got from the UI on a remote linux server and collect the output and display on the Tkinter window.

Regarding the tkinter parts, i am doing fine. the problem is when I wanna execute this command on the remote server and collect the output. I will be running my program in a Virtual Environment of Conda

on a small research on google the most common solution I saw was the usage of fabric as shown here in this link other solutions which I saw was the usage of plink and popen.

But I am not clear as to where I will mention the host name and password to connect to the remote server with any of the solutions stated above. How I will send my command and receive the output back to my tkinter flow. Is there way that I can understand if the command I executed failed because of some reason?

Any help on this will be appreciated! thanks in advance

You want to use host_string to dynamically specify what the destination is. Change it before executing commands.

An example would be:

from fabric.api import run, env

env.host_string = 'user@host.com'

def ls():
    run("ls")

So i found the solution i was looking for.. may be i did not read the docs which i surfed before properly.. here its

def connect():
    print "starting to connect"
    env.host_string = 'vpnuser@10.0.0.70'
    env.password = "vpnuser "
    run("ls -lrth")

put this in a fabfile.py file and run it like this fab -a connect

That is it !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM