Python Forum
Start Putty into Python Form - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Start Putty into Python Form (/thread-25123.html)



Start Putty into Python Form - Schlazen - Mar-20-2020

Hello everyone,

I'm new in this forum and i need help.

I'm a beginner in python programming and i would like to create a Window Form to open SSH Session in tab.
But, i don't how i can put the SSH gui into my tabs of my windows form.

import tkinter as tk

form = tk.Tk()
....

tab_parent = ttk.Notebook(form)
tab_parent.grid(row=2, column=0, columnspan=27, rowspan=2,
               sticky=W+E+N+S, padx=5, pady=5)
        tabServeur = ttk.Frame(tab_parent)
        tab_parent.add(tabServeur, text=TxtServer.get())
...

ConnectButton = Button(form, text='Connect', command=NewTab('arg1', 'arg2'))
ConnectButton.grid(row=1, column=0, columnspan=27,
               sticky=W+E+N+S, padx=5, pady=5)


form.mainloop()
When I click on a "Connect" button, a new tab is created with a ssh Windows (Putty or something else?)

Thank you in advance.


RE: Start Putty into Python Form - Gribouillis - Mar-20-2020

Hello and welcome to the forum.

I don't see a way to embed a Putty window into a tkinter widget, nor other Windows applications such as Cmd or Notepad++ or your web browser.

With a lot of work, you could create a terminal-looking window in tkinter, such as Idle's shell which is written in tkinter and create an SSH session in this windows with the help of specialized modules such as paramiko. Obviously, it is a whole project for a very experienced pythonista.

The simplest method is the best: just call Putty with the help of the subprocess module when the button is clicked and live with a separate Putty window. You'd have a useable program in a matter of minutes.


RE: Start Putty into Python Form - Schlazen - Mar-23-2020

Hi Gribouillis,

Thank you for your help,

I have write this code but i don't know how i can redirect my ssh session into my textbox.
I use Paramiko at the line 38.

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import Menu
import paramiko
import subprocess
import sys
from paramiko_expect import SSHClientInteraction

form = tk.Tk()
form.title("SSHClient")
form.geometry("1000x700")
form.rowconfigure(0, weight=1)
form.rowconfigure(1, weight=1)
form.rowconfigure(2, weight=1)
form.rowconfigure(3, weight=25)
form.columnconfigure(0, weight=1)
form.columnconfigure(1, weight=4)
form.columnconfigure(2, weight=1)
form.columnconfigure(3, weight=4)
form.columnconfigure(4, weight=1)
form.columnconfigure(5, weight=4)
form.columnconfigure(6, weight=1)
form.columnconfigure(7, weight=1)

def NewTab(arg1, arg2):
    def wrap():
        tabServeur = ttk.Frame(tab_parent)
        tab_parent.add(tabServeur, text=TxtServer.get())
        tabServeur.rowconfigure(0, weight=1)
        tabServeur.columnconfigure(0, weight=1)
        #Process = subprocess.Popen('C:\\plink.exe -ssh [email protected] -pw screencast -P 32772',stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        TxtTerm = Text(tabServeur)
        TxtTerm.grid(sticky=W+E+N+S,row=0, column=0, padx=5, pady=5)
        TxtTerm.configure(bg='Black', fg='White')
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname='IP', username='root', password='pwd', port='22')

    return wrap



#Création des boutons
ConnectButton = Button(form, text='Connect', command=NewTab('arg1', 'arg2'))
ConnectButton.grid(row=1, column=0, columnspan=27,
               sticky=W+E+N+S, padx=5, pady=5)

Label(form, text="Server :").grid(row=0, column=0)
Label(form, text="User :").grid(row=0, column=2)
Label(form, text="Password :").grid(row=0, column=4)
Label(form, text="Port :").grid(row=0, column=6)

TxtServer = Entry(form)
TxtServer.grid(row=0, column=1, sticky=W+E, padx=5, pady=5)
TxtUser = Entry(form)
TxtUser.grid(row=0, column=3, sticky=W+E, padx=5, pady=5)
TxtPassword = Entry(form, show="*")
TxtPassword.grid(row=0, column=5, sticky=W+E, padx=5, pady=5)
TxtPort = Entry(form)
TxtPort.grid(row=0, column=7, sticky=W+E, padx=5, pady=5)

#Création des onglets
tab_parent = ttk.Notebook(form)
tab_parent.grid(row=2, column=0, columnspan=27, rowspan=2,
               sticky=W+E+N+S, padx=5, pady=5)

menu = Menu(form)
form.config(menu=menu)
new_item = Menu(menu, tearoff=0)
new_item.add_command(label='Open Keepass')
new_item.add_command(label='Reload')
new_item.add_separator()
new_item.add_command(label='Exit', command=form.quit)
menu.add_cascade(label='File', menu=new_item)
form.config(menu=menu)

form.mainloop()
I have already try to launch putty on a external window, but my target is to have a SSH Shell with tab.

Thank You

Schlazen


RE: Start Putty into Python Form - Gribouillis - Mar-23-2020

If you want to do something with the client, you need to keep a reference to the client, which is not the case here because your client is a local variable of the wrap() function.

The simplest way to interact is to send command lines to the client like so
stdin, stdout, stderr = client.exec_command(command) # <--- get the command from a form perhaps?
status = stdout.channel.recv_exit_status()
for line in stdout:
    print(line) # <--- or write the line to the text window instead
Another solution for a better simulation of interactive ssh sessions is probably to use the paramiko_expect module and try to print the messages sent and received as shown to the Tkinter text window.


RE: Start Putty into Python Form - Schlazen - Mar-25-2020

HI Gribouillis,

It's Work. Thank you for your help.
I will continue to fin a better way to have a better user experience with SSH.

Thank you!


RE: Start Putty into Python Form - divya130 - Dec-13-2022

You can use this application on social media platforms to predict customer behavior and the best product for individual customers. click here data science training in dombivli