I've created a script on python that is initiating as SSH session (between windows and Linux) and allow to execute commands . Now I need to connect the python script with TCL . I need to call this script with some args using TCL , and get a response to TCL of python console .
this is my Python script :
import threading, paramiko from paramiko import client import time class ssh: client = None shell = None
def __init__(self, address, username):
print("Connecting to server.")
cert = paramiko.RSAKey.from_private_key_file("QA-SIP-Evgenyz.pem")
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username=username, pkey=cert)
self.transport = paramiko.Transport((address, 22))
self.transport.connect(username=username, pkey=cert)
thread = threading.Thread(target=self.process)
thread.daemon = True
thread.start()
print("Connected")
def closeConnection(self):
if self.client is not None:
self.client.close()
self.transport.close()
def openShell(self):
self.shell = self.client.invoke_shell()
def sendShell(self, command):
if self.shell:
#print("trying to run command " + command)
self.shell.send(command + "\r")
time.sleep(0.6)
#self.shell.send("\r")
else:
print("Shell not opened.")
def process(self):
global connection
while True:
# Print data when available
if self.shell is not None and self.shell.recv_ready():
alldata = self.shell.recv(1024)
while self.shell.recv_ready():
alldata += self.shell.recv(1024)
strdata = str(alldata, "utf8")
strdata.replace('\r', '')
print(strdata, end = "")
if strdata.endswith("$ "):
print("\n$ ", end = "")
IP = 1.1.1.1
user = ubuntu
connection = ssh("IP", "user")
connection.openShell()
while True:
#Just to test the connection
connection.sendShell("ls -l")
Now I need to find a way to call this Python class via TCL with arguments. I am not sure how the Python class shpuld be written to be able getting the ards from elsewhere . And also, how the "return" works, how can I show the console ?
In your tcl script, you should be able to execute the python code using the exec
call with the arguments you want to pass. You could find more information in the following tcl documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.