linux - 使用 python 脚本运行 linux 命令

标签 linux python-3.x tkinter

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

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

error: 'pwd' is not recognized as an internal or external command, operable program or batch file.

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

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()

最佳答案

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

所以像这样:subprocess.Popen('bash -c pwd')

关于linux - 使用 python 脚本运行 linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008046/

相关文章:

python - 列表的列表..列表的列表?应用正则表达式和 nltk

python - 如何使用跟踪 "live"更新 matplotlib 图形图

python - 自动更新无法直接访问的 tkinter 标签

linux - 无滴答内核、isolcpus、nohz_full 和 rcu_nocbs

c# - Mono 3.2 中的行为与 Nancy Web 解决方案上的 DotLiquid 集合不一致

linux - shell字符串和程序执行

python-3.x - 无法在 Tkinter 中移动小部件

c - 使用 execvp() 调用 Linux ps 命令

python - 如何在python中显示表格中每一行和每一列的总和

python - 您可以动态地向对象添加属性吗?