使用python脚本运行linux命令

时间:2019-01-02 14:24:33

标签: linux python-3.x tkinter

我用Tkinter创建了GUI python,并通过SSH与服务器连接,一切都很好,但是当我使用subprocess时,它给了我错误:

import subprocess
subprocess.Popen('pwd',shell=True)
  
    

错误:         无法将“ pwd”识别为内部或外部命令,         可操作的程序或批处理文件。

  
     

如何将结果设置到文本框中?

import tkinter as tk
from tkinter import messagebox
from tkinter import *
from  getpass import *
from socket import *
import paramiko
import sys
import time
import socket 
import platform
import subprocess 
import os
import os
class LoginFrame(Frame):

def __init__(self, master):
    super().__init__(master)
    self.label_ipa=Label(self, text="IP Address",font=("Times New Roman",14), fg="blue")
    self.label_username = Label(self, text="Username",font=("Times New Roman", 14), height=4, fg="blue")
    self.label_password = Label(self, text="Password",font=("Times New Roman", 14), height=4, fg="blue")
    self.entry_ipa = Entry(self,justify=CENTER)#.grid(row-0,column=1)
    self.entry_username = Entry(self,justify=CENTER)#.grid(row=0,column=1,pady=4)
    self.entry_password = Entry(self, show="*",justify=CENTER)
    self.label_ipa.grid(row=0,sticky=E)
    self.label_username.grid(row=1, sticky=E)
    self.label_password.grid(row=2, sticky=E)
    self.entry_ipa.grid(row=0,column=1)
    self.entry_username.grid(row=1, column=1)
    self.entry_password.grid(row=2, column=1)
    self.disct = Button(self, text="Close Session",command=self._disc_,font=("Times New Roman", 14), width=10,height=2, fg="red")
    self.logbtn = Button(self, text="Connecting", command=self._login_,font=("Times New Roman", 14), width=10,height=2, fg="green")
    self.logbtn.grid(row=3,column=0)
    self.disct.grid(row=3,column=2)
    self.pack()

def _login_(self):

            ipa = self.entry_ipa.get()

            username = self.entry_username.get()
            password = self.entry_password.get()

            if not ipa:
                tk.messagebox.showerror("Error MessageBox","please Enter the IP Address",icon="warning")
                return False

            if not username:
                tk.messagebox.showerror("Error MessageBox","please Enter Your Username",icon="warning")
                return False
            if not password:
                tk.messagebox.showerror("Error MessageBox","please Enter Your Password",icon="warning")
                return False

            cl1=paramiko.SSHClient()
            cl1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            cl1.connect(ipa,username=username,password=password)

            if(ipa==ipa and username==username and password==password):

                    tk.messagebox.showinfo("OPEN THE Session","SSH connection to %s established" %ipa)

            else:
    #client_key
                    tk.messagebox.showerror("Error MessageBox","please try again",icon="warning")
            window = Toplevel(root)
            window.title("Command Action")
            window.geometry("400x350+400+350")
            b1=Button(window,text="check Memory",bg="red").place(x=0,y=0)
            b2=Button(window,text="Free disk Space",bg="red").place(x=100,y=0)
            b3=Button(window,text="Imprt XML FILE",bg="red").place(x=220,y=0)
            text=Text(window,height=10,width=50).place(x=0,y=50)
            subprocess.Popen('pwd',shell=True)
def _disc_(self):

      MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want Dissconnecting ',icon = 'warning')

      if MsgBox == "yes":

          root.destroy()

      else:

          tk.messagebox.showinfo('Return','You will now return to the 
           application screen')

root = tk.Tk()
root.geometry('400x400+50+50')
root.title('Connecting to')
lf = LoginFrame(root)
root.mainloop()

3 个答案:

答案 0 :(得分:1)

您可以在shell中运行它。 pwd来自我相信的外壳。

是这样的:subprocess.Popen('bash -c pwd')

答案 1 :(得分:0)

  

我用我自己解决了问题

stdin, stdout, stderr=cl1.exec_command("free -g")
            print(stdout.read())

现在一切都很好,请告诉我我要将结果插入文本框*

答案 2 :(得分:0)

为什么首先要启动pwd命令?更改目录时,您可以修改PWD环境变量,而命令pwd仅要求提供此环境变量。这样的东西(未经测试)应该可以解决您的问题:

import os
print(os.environ['PWD'])