如何将 TCL 连接到 Python
How to connect TCL to Python
我在 python 上创建了一个脚本,它作为 SSH 会话(在 windows 和 Linux 之间)启动并允许执行命令。
现在我需要将 python 脚本与 TCL 连接起来。
我需要使用 TCL 使用一些参数调用此脚本,并获得对 python console 的 TCL 的响应。
这是我的 Python 脚本:
导入线程,paramiko
来自 paramiko 导入客户端
导入时间
class ssh:
客户 = 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")
现在我需要找到一种通过 TCL 带参数调用此 Python class 的方法。
我不确定 Python class 应该如何编写才能从其他地方获取 ards。
还有,"return" 是如何工作的,我怎样才能显示控制台?
在您的 tcl 脚本中,您应该能够使用带有您要传递的参数的 exec
调用来执行 python 代码。您可以在以下 tcl documentation.
中找到更多信息
我在 python 上创建了一个脚本,它作为 SSH 会话(在 windows 和 Linux 之间)启动并允许执行命令。 现在我需要将 python 脚本与 TCL 连接起来。 我需要使用 TCL 使用一些参数调用此脚本,并获得对 python console 的 TCL 的响应。
这是我的 Python 脚本:
导入线程,paramiko 来自 paramiko 导入客户端 导入时间 class ssh: 客户 = 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")
现在我需要找到一种通过 TCL 带参数调用此 Python class 的方法。 我不确定 Python class 应该如何编写才能从其他地方获取 ards。 还有,"return" 是如何工作的,我怎样才能显示控制台?
在您的 tcl 脚本中,您应该能够使用带有您要传递的参数的 exec
调用来执行 python 代码。您可以在以下 tcl documentation.