python远程管理编程中如何处理Fn(功能)键

liftword1周前 (03-05)技术文章1

我之前管理过一款通讯设备,是需要登录远程管理客户端后才能进行操作的。这个设备的远程管理软件的功能挺多,但是主要的设备管理操作还是在一个单调的字符界面下完成的,后来经其他工程师提醒,我才意识到此时该软件就相当于一个“封装”后Telnet客户端(终端)。

由于这个“带有迷惑性”的客户端,再加上大家的操作习惯,平时大家的操作都是手工完成的,即使每天都有重复的工作,即使每次切换都是相同的操作步骤。手工操作不仅耗时长,而且易出错,切换完以后还要逐一核对切换后的信息,每次这一套流程做完,差不多十来分钟就过去了!

接触python以后,我就希望能通过python程序来“自动化/批量”处理这样的常规操作,而且不仅可以实现python程序“自动化”切换,也能对平时的设置进行检查。找到一款可以实现telnet功能的python模块并不难,连接设备并且检查系统设置也没有遇到多大问题,但是在执行设备“切换”操作步骤的时候,我遇到了“拦路虎”——客户端操作需要使用F1等多个功能键,才能修改初始数据并提交给设备。

我试着通过搜索引擎搜索,试着检索ascii码表,都没有找到有用的信息,就在“迷茫无措”时,偏偏让我从Putty客户端的帮助文件中找到了相关的信息:

  • In the default mode, labelled ESC [n~, the function keys generate sequences like ESC [11~, ESC [12~ and so on. This matches the general behaviour of Digital's terminals.
  • In Linux mode, F6 to F12 behave just like the default mode, but F1 to F5 generate ESC [[A through to ESC [[E. This mimics the Linux virtual console.
  • In Xterm R6 mode, F5 to F12 behave like the default mode, but F1 to F4 generate ESC OP through to ESC OS, which are the sequences produced by the top row of the keypad on Digital's terminals.
  • In VT400 mode, all the function keys behave like the default mode, but the actual top row of the numeric keypad generates ESC OP through to ESC OS.
  • In VT100+ mode, the function keys generate ESC OP through to ESC O[
  • In SCO mode, the function keys F1 to F12 generate ESC [M through to ESC [X. Together with shift, they generate ESC [Y through to ESC [j. With control they generate ESC [k through to ESC [v, and with shift and control together they generate ESC [w through to ESC [{.

PuTTY responds to function key presses by sending a sequence of control characters to the server.

……

The simplest way to investigate this is to find some other terminal environment, in which that function key does work; and then investigate what sequence the function key is sending in that situation. One reasonably easy way to do this on a Unix system is to type the command cat, and then press the function key. This is likely to produce output of the form ^[[11~. You can also do this in PuTTY, to find out what sequence the function key is producing in that.

也就是说,在python程序中需要通过发送一段特定的字符串,比如ESC [11~……等来实现功能键的录入,具体的字符串需要与python程序模拟的telnet终端的类型(VT100等)相匹配。我们可以在Unix/Linux终端中,执行命令cat,在命令中按下功能键Fn,实际输出结果就刚提交的字符串组合,比如ESC键显示为“^[”,F6显示“^[[17~”……,当然我们更可以在设备终端中执行以上的操作,查看功能键对应的字符串组合。最后大家还是要通过在python程序中的测试,来验证获取的Fn功能键录入字符串是否正确、有效!

好了,这就是我曾经遇到的一个“拦路虎”,“突破”以后分析给大家,希望能对大家有所帮助。

相关文章

Python 命令行工具 python 的常用参数执行命令

作为 Python 的初学者,最不缺见的就是命令行工具 python 的执行命令了,每每遇到就可能去查资料帮助,同样,自己也会不时的需要某些执行命令来完成自己的需求,鉴于此我对 python 工具的执...

python中执行shell命令的几个方法小结!很实用,帮助很大

最近有个需求就是页面上执行shell命令,第一想到的就是os.system,os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。...

如何在 Python 中执行外部命令 ?

Python 是一种强大的编程语言,可以帮助自动执行许多任务,包括在 Linux 系统上运行命令。在本指南的最后,您将能够使用 Python 轻松有效地执行 Linux 命令。使用 os 模块os 模...

Python 中的一些命令行命令

虽然 Python 通常用于构建具有图形用户界面 (GUI) 的应用程序,但它也支持命令行交互。命令行界面 (CLI) 是一种基于文本的方法,用于与计算机的操作系统进行交互并运行程序。从命令行运行 P...

入门必学25个python常用命令

以下是 Python 入门必学的 25 个常用命令(函数、语句等):基础输入输出与数据类型print():用于输出数据到控制台,例如print("Hello, World!")。input():获取用...

Python 基础教程 九之cron定时执行python脚本

前言在Linux或Unix系统中,你可以使用cron任务来定时执行Python脚本。cron是一个基于时间的作业调度器,允许你安排命令或脚本在系统上自动执行。安装cron大多数Linux发行版默认安装...