使用python中paramiko发送命令行时需要发送"ctrl+c"命令
举例:先执行top命令,再执行"ctrl+c",最后执行pkill top
def __execute_ssh_cmd(self):
try:
command = self.case.get("ssh_cmd")
command = command.removeprefix(" ")
PyLog().set_log("【执行cmd函数】:" + command)
if command and command.startswith('scp'):
ftp = self.ssh_client.client.open_sftp()
_, server_file, local_file = tuple(command.split(";"))
ftp.get(server_file, local_file)
PyLog().set_log("【cmd执行结果】:" + "文件下载成功")
print("文件下载成功")
else:
for command in command.split(';'):
stdin, stdout, stderr = self.ssh_client.client.exec_command(command, timeout=5, get_pty=True)
time.sleep(2)
result = stdout.read(100)
PyLog().set_log("【cmd执行结果】:" + result.decode('utf-8'))
print(result)
except Exception as e:
# errr=stderr.read(100)
PyLog().set_log("【cmd执行结果】:" + str(e))
print(e)
return
在执行nc -nv -l -p 11190 -s 0.0.0.0 ; ^C时result返回没有报错信息
在执行top ;^C ; pkill top中的^C时mresult中返回not found command
使用^C模拟ctrl+c
传入某个字符串用以模拟ctrl+C
搞不懂你到底是出了几个问题,总之发现两个你说的问题,给你说一下
关于 Ctrl +c 你可以使用 paramiko.hotkey('ctrl', 'c') 来执行,另外关于你的那个pkill top,这个是杀死进程的命令,你需要用pkill -9 后面加程序进程的pid,或者直接pkill后面加进程名也可以。