Python Forum
Start Putty into Python Form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Start Putty into Python Form
#1
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.
Reply
#2
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.
Reply
#3
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
Reply
#4
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.
Reply
#5
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!
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,696 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Python Idle won't start totalmachine 9 3,381 Oct-16-2022, 05:57 PM
Last Post: totalmachine
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,473 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  Error creating database with python and form? shams 3 2,328 Aug-02-2021, 02:00 PM
Last Post: deanhystad
  Error using mariadb select query with form in python? shams 2 1,956 Jul-29-2021, 12:30 PM
Last Post: shams
  html, python, a simple form test 1 1,900 Aug-09-2020, 01:59 PM
Last Post: snippsat
  Reading text from Putty window Nil 5 9,085 Jul-23-2020, 10:27 AM
Last Post: HritikaMehta
  Use Python to start/stop a server service via a webform? oakleaf2001 0 1,724 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  Python 3.8 on mac failing to start sgandon 0 2,842 Jan-14-2020, 10:58 AM
Last Post: sgandon
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,615 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020