Network
Network

Reputation: 1

run linux command with python script

I created GUI python with Tkinter and connect with the server via SSH and everything is good, but when I'm using the subprocess it give me error:

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

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

How can I set the result into Textbox?

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

Upvotes: 0

Views: 447

Answers (3)

Dominique
Dominique

Reputation: 17565

Why do you launch the pwd command in the first place? When changing directory, you modify the PWD environment variable, and the command pwd just asks for this environment variable. Something like this (not tested) should solve your issue:

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

Upvotes: 0

Network
Network

Reputation: 1

i resolved my problem by myself, i used

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

now everything is good, please if you can advise me that i want insert the result into the text box*

Upvotes: 0

codebreach
codebreach

Reputation: 2220

You can run it in a shell. pwd comes from the shell i believe.

So something like this: subprocess.Popen('bash -c pwd')

Upvotes: 1

Related Questions