在python Paramiko ssh上发送enter键
我正在尝试创建一个函数,通过minicom从另一台机器连接到另一台机器。连接到minicom后,应按下enter键,以便向minicom连接的机器发送命令。我的python代码如下所示:在python Paramiko ssh上发送enter键,python,ssh,Python,Ssh,我正在尝试创建一个函数,通过minicom从另一台机器连接到另一台机器。连接到minicom后,应按下enter键,以便向minicom连接的机器发送命令。我的python代码如下所示: ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.serialHost, username=self.
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.serialHost, username=self.username, password=self.password)
shell = ssh.invoke_shell()
shell.send('minicom free -o')
shell.send('\u000d')
ssh.close()
有人能告诉我如何正确发送回车键吗?您可以尝试对返回键执行
'\r\n'
,但我认为还有一个exec\u命令
方法调用不需要返回键。差不多
shell.exec_command('minicom free -o')
通常,当尝试在paramiko中执行命令时,您不必调用shell,只需调用
ssh.exec_命令(…)
。如果要执行的命令取决于启动shell所提供的环境,那么必须显式调用invoke\u shell()
方法
在paramiko中使用invoke_shell()
时,必须发送特定shell所需的行终止字符。如果要使用ssh连接的机器的默认shell为bash
,则必须在每个命令后发送换行符(即'\n'
)。例如:
shell.send('ls\n')
而不是
shell.send('ls')
如果要连接到较旧的Windows计算机,则需要发送回车符和换行符(即'\r\n'
)才能处理该命令