将python代码与Visual Studio c#集成

问题描述 投票:0回答:1

我已经在Visual Studio 2019中创建了GuI。

enter image description here

[用户将输入用户名和密码,我必须将其传递给python脚本。当用户单击登录按钮时,将触发python脚本并显示输出。

我尝试过的python代码是:

import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:

    hostname = input("Enter host IP address: ")
    username = input("Enter SSH Username: ")
    password = input("Enter SSH Password: ")
    port = 22


    ssh.connect(hostname, port, username, password, look_for_keys=False)
    print("ssh login successfully")



#stdin,stdout,stderr = ssh.exec_command('show version')
#output = stdout.readlines()
#print(output)


    Device_access = ssh.invoke_shell()
    Device_access.send(b'environment no more \n')
    Device_access.send(b'show version\n')
    time.sleep(2)
    output = Device_access.recv(65000)
    print (output.decode('ascii'))

except:
    print("error in connection due to wrong input entered")

但是在此我没有得到如何与输入链接的方法,请使用python脚本输入Gui c#。请让我知道我该怎么做。

提前感谢!

python c# .net visual-studio windows-forms-designer
1个回答
0
投票
更改python脚本:

import paramiko import time import sys # Used to get arguments ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: hostname = sys.args[1] # Skip 0th arg, since it is just the filename username = sys.args[2] password = sys.args[3] port = 22 ssh.connect(hostname, port, username, password, look_for_keys=False) print("ssh login successfully") #stdin,stdout,stderr = ssh.exec_command('show version') #output = stdout.readlines() #print(output) Device_access = ssh.invoke_shell() Device_access.send(b'environment no more \n') Device_access.send(b'show version\n') time.sleep(2) output = Device_access.recv(65000) print (output.decode('ascii')) except: print("error in connection due to wrong input entered")

然后将调用脚本的C#代码更改为如下代码:

Process pythonScript = new Process();
pythonScript.StartInfo.FileName = "Your python script";
pythonScript.StartInfo.Arguments = $"{YouHostnameVar} {YouUsernameVar} {YourPasswordVar}"; // Start the script with the credentials as arguments
pythonScript.Start();
© www.soinside.com 2019 - 2024. All rights reserved.