CN113190296A - Paramiko-based interactive command execution method and device - Google Patents

Paramiko-based interactive command execution method and device Download PDF

Info

Publication number
CN113190296A
CN113190296A CN202110545509.4A CN202110545509A CN113190296A CN 113190296 A CN113190296 A CN 113190296A CN 202110545509 A CN202110545509 A CN 202110545509A CN 113190296 A CN113190296 A CN 113190296A
Authority
CN
China
Prior art keywords
command
execute
function
expect
interactive
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110545509.4A
Other languages
Chinese (zh)
Inventor
赵万成
苗健
卢健
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Highgo Base Software Co ltd
Original Assignee
Highgo Base Software Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Highgo Base Software Co ltd filed Critical Highgo Base Software Co ltd
Priority to CN202110545509.4A priority Critical patent/CN113190296A/en
Publication of CN113190296A publication Critical patent/CN113190296A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/448Execution paradigms, e.g. implementations of programming paradigms
    • G06F9/4482Procedural
    • G06F9/4484Executing subprograms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/448Execution paradigms, e.g. implementations of programming paradigms
    • G06F9/4488Object-oriented
    • G06F9/449Object-oriented method invocation or resolution

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • User Interface Of Digital Computer (AREA)

Abstract

The invention provides a method and a device for executing an interactive command based on Paramiko, wherein the method for executing the interactive command based on Paramiko comprises the following steps: based on the shell class, defining an execute function, which comprises the following steps: a first execute function and a second execute function; executing the first execute function, and judging whether the executed command is a non-interactive command or an interactive command based on a first execute in the first execute function; when the executed command is judged to be an interactive command, calling a _ recv _ uniform function in the shell class, and returning a playback character string when the specified state of the _ recv _ uniform function is the first expect preset content; and executing the second execute function, and finishing the execution of the interactive command when the second execute in the second execute function is '0'. The invention can simultaneously execute the commands for executing the interactive commands and the non-interactive commands, avoids the inconvenience caused by the fact that Paramiko cannot execute the interactive commands, and can greatly improve the degree of automation control of the remote server realized by the program.

Description

Paramiko-based interactive command execution method and device
Technical Field
The invention relates to the technical field of communication, in particular to a method and a device for executing an interactive command based on Paramiko.
Background
Paraamiko is a Python language implementation of SSH (Secure Shell Security Shell) protocol, which can be used as an SSH client to connect to an SSH server and then execute commands.
An interactive command, that is, a command, needs to be continued after a user inputs necessary information according to a prompt in the execution process, for example, a command for deleting a file prompts the user whether to confirm deletion in the execution process, and the deletion operation is executed only after the user inputs yes confirmation. However, the method of Paramiko for executing commands cannot execute interactive commands.
Disclosure of Invention
The invention provides a method and a device for executing an interactive command based on Paramiko.
The Paramiko-based interactive command execution method comprises the following steps:
based on the shell class, defining an execute function, which comprises the following steps: a first execute function and a second execute function;
executing the first execute function, and judging whether the executed command is a non-interactive command or an interactive command based on a first execute in the first execute function;
when the executed command is judged to be an interactive command, calling a _ recv _ uniform function in the shell class, and returning a playback character string when the specified state of the _ recv _ uniform function is the first expect preset content;
and executing the second execute function, and finishing the execution of the interactive command when the second execute in the second execute function is '0'.
According to some embodiments of the present invention, when the first execute is executed, when the first execute is ' 0 ' or ',
and calling a _ recv _ unity function in the shell class, and returning a playback character string when the designated state of the _ recv _ unity function is that command execution is finished.
In some embodiments of the present invention, the _ is _ cmd _ done function is used to determine whether the command execution is finished.
According to some embodiments of the invention, when the second expect is '0', the method further comprises:
calling _ get _ RC to obtain a return code, checking whether the return code is NOEER _ RC or not, and if not, performing error reporting;
and calling _ wash _ rs to wash the echo character string, and returning the washed character string as a return value.
In some embodiments of the invention, the parameters of the execute function include:
self, for identifying the class instance itself;
the command is used for inputting a command character string to be executed;
expect, which is an expected character string, when expect is '0', it indicates that no error is expected; when expect is "indicating that command execution is expected to be completed and no error is checked; when expect is contents other than ' 0 ' and ', it indicates that the specified contents appear in the desired display;
timeout for defining a command timeout time;
chan, for specifying Channel.
The Paramiko-based interactive command execution device according to the embodiment of the invention comprises:
the definition module is used for defining an execute function based on the shell class and comprises the following components: a first execute function and a second execute function;
the interactive command judging module is used for executing the first execute function and judging whether the executed command is a non-interactive command or an interactive command based on the first execute in the first execute function;
the first execution module is used for calling a _ recv _ unity function in the shell class when the executed command is judged to be an interactive command, and returning a echoed character string when the specified state of the _ recv _ unity function is the first expect preset content;
and the second execution module is used for executing the second execute function, and when the second execute in the second execute function is '0', the execution of the interactive command is completed.
According to some embodiments of the invention, the apparatus further comprises:
the error reporting module is used for calling _ get _ RC to obtain a return code, checking whether the return code is NOEER- _ RC or not, and if not, performing error reporting;
and the cleaning module is used for calling the _ wash _ rs to clean the echo character string and returning the cleaned character string as a return value.
In some embodiments of the invention, the parameters of the execute function include:
self, for identifying the class instance itself;
the command is used for inputting a command character string to be executed;
expect, which is an expected character string, when expect is '0', it indicates that no error is expected; when expect is "indicating that command execution is expected to be completed and no error is checked; when expect is contents other than ' 0 ' and ', it indicates that the specified contents appear in the desired display;
timeout for defining a command timeout time;
chan, for specifying Channel.
The electronic device according to an embodiment of the present invention at least includes a memory and a processor, where the memory stores a computer program, and the processor implements the steps of the method for executing an interactive command based on Paramiko when executing the computer program on the memory.
According to an embodiment of the present invention, a computer storage medium is stored with a computer program, which when executed by a processor implements the steps of the method for executing Paramiko-based interactive commands as described above.
The Paramiko-based interactive command execution method, the device, the electronic equipment and the computer storage medium have the following beneficial effects:
in any command line, the use of interactive commands is not completely avoidable, if the interactive commands cannot be executed by a program, a plurality of operations cannot be realized by the program, and great inconvenience is brought.
Drawings
FIG. 1 is a flowchart of a Paramiko-based interactive command execution method according to an embodiment of the present invention;
FIG. 2 is a diagram illustrating attributes and methods of shell classes according to an embodiment of the present invention;
fig. 3 is a flowchart of execute method according to an embodiment of the present invention.
Detailed Description
To further explain the technical means and effects of the present invention adopted to achieve the intended purpose, the present invention will be described in detail with reference to the accompanying drawings and preferred embodiments.
The description of the method flow in the present specification and the steps of the flow chart in the drawings of the present specification are not necessarily strictly performed by the step numbers, and the execution order of the method steps may be changed. Moreover, certain steps may be omitted, multiple steps may be combined into one step execution, and/or a step may be broken down into multiple step executions.
As shown in fig. 1, the method for executing a Paramiko-based interactive command according to an embodiment of the present invention includes:
s110, defining an execute function based on the shell class, wherein the execute function comprises the following steps: a first execute function and a second execute function;
s120, executing a first execute function, and judging whether the executed command is a non-interactive command or an interactive command based on a first execute in the first execute function;
s130, when the executed command is judged to be an interactive command, calling a _ recv _ uniform function in the shell class, and when the specified state of the _ recv _ uniform function is the first expect preset content, returning a playback character string;
and S140, executing the second execute function, and finishing the execution of the interactive command when the second execute in the second execute function is '0'.
Referring to fig. 2, in accordance with some embodiments of the present invention, when the first execute is executed, when the first execute is ' 0 ' or ',
and calling a _ recv _ unity function in the shell class, and returning a echoed character string when the specified state of the _ recv _ unity function is that the command execution is finished. For example, the _ is _ cmd _ done function may be used to determine whether the command execution is finished.
According to some embodiments of the invention, as shown in fig. 2, when the second expect is '0', the method further includes:
calling _ get _ RC to obtain a return code, checking whether the return code is NOEER _ RC or not, and if not, performing error reporting;
and calling _ wash _ rs to wash the echo character string, and returning the washed character string as a return value.
In some embodiments of the present invention, as shown in FIG. 3, the parameters of execute function include:
self, for identifying the class instance itself;
the command is used for inputting a command character string to be executed;
expect, which is an expected character string, when expect is '0', it indicates that no error is expected; when expect is "indicating that command execution is expected to be completed and no error is checked; when expect is contents other than ' 0 ' and ', it indicates that the specified contents appear in the desired display;
timeout for defining a command timeout time;
chan, for specifying Channel.
The Paramiko-based interactive command execution device according to the embodiment of the invention comprises: the device comprises a definition module, an interactive command judgment module, a first execution module and a second execution module.
The definition module is used for defining an execute function based on the shell class, and comprises the following steps: a first execute function and a second execute function;
the interactive command judging module is used for executing the first execute function and judging whether the executed command is a non-interactive command or an interactive command based on the first execute in the first execute function;
the first execution module is used for calling a _ recv _ unity function in the shell class when the executed command is judged to be the interactive command, and returning a echoed character string when the specified state of the _ recv _ unity function is the first expect preset content;
the second execution module is used for executing a second execute function, and when a second expect in the second execute function is '0', the execution of the interactive command is completed.
According to some embodiments of the invention, the apparatus further comprises: the device comprises an error reporting module and a cleaning module.
The error reporting module is used for calling _ get _ RC to obtain a return code, checking whether the return code is NOEER- _ RC or not, and if not, performing error reporting;
and the cleaning module is used for calling the _ wash _ rs to clean the echo character string and returning the cleaned character string as a return value.
The electronic device according to an embodiment of the present invention at least includes a memory and a processor, where the memory stores a computer program, and is characterized in that the processor implements the steps of the method for executing an interactive command based on Paramiko when executing the computer program on the memory, and specifically includes:
a10, defining an execute function based on the shell class, comprising: a first execute function and a second execute function;
a20, executing a first execute function, and judging whether the executed command is a non-interactive command or an interactive command based on a first execute in the first execute function;
when the first expect is '0' or ', judging the executed command to be a non-interactive command, and when the first expect is' 0 'or' other contents, judging the executed command to be an interactive command;
a30, calling a _ recv _ unitl function in the shell class when the executed command is judged to be an interactive command, and returning a echoed character string when the specified state of the _ recv _ unitl function is the first expect preset content;
and when the executed command is judged to be a non-interactive command, calling a _ recv _ unity function in the shell class, and returning a playback character string when the specified state of the _ recv _ unity function is that the command execution is finished. For example, the _ is _ cmd _ done function may be used to determine whether the command is executed and ended;
a40, executing a second execute function, calling _ get _ RC to obtain a return code when a second expect in the second execute function is '0', and checking whether the return code is NOEER _ RC, if not, performing error reporting;
and A50, calling _ wash _ rs to wash the echo character string, and returning the washed character string as a return value. And completing the execution of the interactive command.
According to the computer storage medium of the embodiment of the present invention, the computer storage medium stores a computer program, and when the computer program is executed by a processor, the method for executing the Paramiko-based interactive command includes:
a10, defining an execute function based on the shell class, comprising: a first execute function and a second execute function;
a20, executing a first execute function, and judging whether the executed command is a non-interactive command or an interactive command based on a first execute in the first execute function;
when the first expect is '0' or ', the executed command is judged to be a non-interactive command, and when the first expect is' 0 'or' other content, the executed command is judged to be an interactive command.
A30, calling a _ recv _ unitl function in the shell class when the executed command is judged to be an interactive command, and returning a echoed character string when the specified state of the _ recv _ unitl function is the first expect preset content;
a40, executing a second execute function, calling _ get _ RC to obtain a return code when a second expect in the second execute function is '0', and checking whether the return code is NOEER _ RC, if not, performing error reporting;
and A50, calling _ wash _ rs to wash the echo character string, and returning the washed character string as a return value. And completing the execution of the interactive command.
The method and apparatus for executing the Paramiko-based interactive command according to the present invention will be described in detail with reference to the accompanying drawings in a specific embodiment. It is to be understood that the following description is only exemplary in nature and should not be taken as a specific limitation on the invention.
Before the SSH comes to be born, people use Telnet protocol to perform remote login and command execution, but because Telnet protocol uses plaintext to transmit data, there is a great security problem.
SSH is just one protocol, and different programming languages implement it specifically. Paramiko is the most widely used SSH protocol implementation in Python language, and is typically used in programs for remote login and command execution. The Paramiko execution command is realized by an exec _ command method of a Channel type, and the method realizes the operation of remotely executing the command by sending a message of type 'exec' to the Channel connected to the server and carrying a command line character string. However, according to the SSH protocol, this type of message can only carry a non-interactive command, i.e. a command that the user does not need to input other information in the execution process, and if the message carries an interactive command, the execution process will be stuck and cannot be finished all the time in the step of waiting for the user to input.
In order to solve the above problems, the present invention proposes a device that can execute both non-interactive commands and interactive commands by using the send and recv methods of the Channel class of Paramiko, i.e. a class named Shell, which provides a method named execute for executing commands.
Specifically, the invention is a class named Shell, and the main attributes and methods of the class are shown in the following figures:
the most core method of the Shell class is execute, and other methods beginning with an underline "_" are indicated as internal methods of the class for internal use only, and the internal methods are all used for execute service, so the most key point of the invention is how the execute method is implemented, and execute is defined as follows:
def execute(self,command,expect=’0’,timeout=10,chan=None);
wherein, the parameter self: the first parameter of the class method in the Python language is in a fixed format and represents the class instance itself;
parameter command: a command string to be executed;
parameter expect: the expected character string, ' 0 ' indicates that no error is expected, ' indicates that completion of command execution is expected but whether there is an error is not checked, and the others indicate that the specified contents are expected to appear in the playback;
the parameter timeout: command timeout time, unit of second, if the command has not been finished within the time, the command will be forced to be finished;
the parameter chan: assigning Channel names, one Channel corresponding to one terminal, and using a default Channel if not assigned;
the flow of Execute execution method is shown in fig. 2. For ease of understanding, the following are presented in three specific examples:
executing a non-interactive command and checking if an error is executed:
execute(‘mkdir/home/me/newdir’,expect=’0’)
execute a non-interactive command without checking if an error is executed:
execute(‘cd/not/exist/dir’,expect=”)
executing an interactive command:
execute(‘rm-i testfile’,expect=”remove regular file‘testfile’?“)
execute(‘y’,expect=’0’)
in summary, in any command line, the use of interactive commands is not completely avoidable, and if the interactive commands cannot be executed by a program, many operations cannot be realized by the program, which brings great inconvenience.
While the invention has been described in connection with specific embodiments thereof, it is to be understood that it is intended by the appended drawings and description that the invention may be embodied in other specific forms without departing from the spirit or scope of the invention.

Claims (10)

1. A method for executing a Paramiko-based interactive command, comprising:
based on the shell class, defining an execute function, which comprises the following steps: a first execute function and a second execute function;
executing the first execute function, and judging whether the executed command is a non-interactive command or an interactive command based on a first execute in the first execute function;
when the executed command is judged to be an interactive command, calling a _ recv _ uniform function in the shell class, and returning a playback character string when the specified state of the _ recv _ uniform function is the first expect preset content;
and executing the second execute function, and finishing the execution of the interactive command when the second execute in the second execute function is '0'.
2. The method of Paramiko-based interactive command execution according to claim 1, wherein, when the first execute is executed, when the first execute is ' 0 ' or ',
and calling a _ recv _ unity function in the shell class, and returning a playback character string when the designated state of the _ recv _ unity function is that command execution is finished.
3. The method of claim 2, wherein an is _ cmd _ done function is used to determine whether the command is executed.
4. The method of Paramiko-based interactive command execution according to claim 1, wherein when the second expect is '0', the method further comprises:
calling _ get _ RC to obtain a return code, checking whether the return code is NOEER _ RC or not, and if not, performing error reporting;
and calling _ wash _ rs to wash the echo character string, and returning the washed character string as a return value.
5. The method of claim 1, wherein the parameters of the execute function include:
self, for identifying the class instance itself;
the command is used for inputting a command character string to be executed;
expect, which is an expected character string, when expect is '0', it indicates that no error is expected; when expect is "indicating that command execution is expected to be completed and no error is checked; when expect is contents other than ' 0 ' and ', it indicates that the specified contents appear in the desired display;
timeout for defining a command timeout time;
chan, for specifying Channel.
6. A Paramiko-based interactive command execution apparatus, comprising:
the definition module is used for defining an execute function based on the shell class and comprises the following components: a first execute function and a second execute function;
the interactive command judging module is used for executing the first execute function and judging whether the executed command is a non-interactive command or an interactive command based on the first execute in the first execute function;
the first execution module is used for calling a _ recv _ unity function in the shell class when the executed command is judged to be an interactive command, and returning a echoed character string when the specified state of the _ recv _ unity function is the first expect preset content;
and the second execution module is used for executing the second execute function, and when the second execute in the second execute function is '0', the execution of the interactive command is completed.
7. The Paramiko-based interactive command execution apparatus of claim 6, further comprising:
the error reporting module is used for calling _ get _ RC to obtain a return code, checking whether the return code is NOEER- _ RC or not, and if not, performing error reporting;
and the cleaning module is used for calling the _ wash _ rs to clean the echo character string and returning the cleaned character string as a return value.
8. The apparatus of claim 6, wherein the parameters of the execute function include:
self, for identifying the class instance itself;
the command is used for inputting a command character string to be executed;
expect, which is an expected character string, when expect is '0', it indicates that no error is expected; when expect is "indicating that command execution is expected to be completed and no error is checked; when expect is contents other than ' 0 ' and ', it indicates that the specified contents appear in the desired display;
timeout for defining a command timeout time;
chan, for specifying Channel.
9. An electronic device comprising at least a memory, a processor, said memory having a computer program stored thereon, wherein said processor, when executing the computer program on said memory, implements the steps of the method for Paramiko-based interactive command execution according to any of claims 1 to 5.
10. A computer storage medium having stored thereon a computer program which, when executed by a processor, carries out the steps of the method of Paramiko-based interactive command execution according to any of claims 1 to 5.
CN202110545509.4A 2021-05-19 2021-05-19 Paramiko-based interactive command execution method and device Pending CN113190296A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110545509.4A CN113190296A (en) 2021-05-19 2021-05-19 Paramiko-based interactive command execution method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110545509.4A CN113190296A (en) 2021-05-19 2021-05-19 Paramiko-based interactive command execution method and device

Publications (1)

Publication Number Publication Date
CN113190296A true CN113190296A (en) 2021-07-30

Family

ID=76982482

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110545509.4A Pending CN113190296A (en) 2021-05-19 2021-05-19 Paramiko-based interactive command execution method and device

Country Status (1)

Country Link
CN (1) CN113190296A (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107291524A (en) * 2016-03-31 2017-10-24 阿里巴巴集团控股有限公司 A kind for the treatment of method and apparatus of remote command
US20180041470A1 (en) * 2016-08-08 2018-02-08 Talari Networks Incorporated Applications and integrated firewall design in an adaptive private network (apn)
CN111343015A (en) * 2020-02-18 2020-06-26 中国银联股份有限公司 Operation and maintenance method and device for network equipment
CN111880903A (en) * 2020-07-30 2020-11-03 浪潮云信息技术股份公司 Cloud platform virtual machine image automatic manufacturing, testing and publishing device and method based on DevOps

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107291524A (en) * 2016-03-31 2017-10-24 阿里巴巴集团控股有限公司 A kind for the treatment of method and apparatus of remote command
US20180041470A1 (en) * 2016-08-08 2018-02-08 Talari Networks Incorporated Applications and integrated firewall design in an adaptive private network (apn)
CN111343015A (en) * 2020-02-18 2020-06-26 中国银联股份有限公司 Operation and maintenance method and device for network equipment
CN111880903A (en) * 2020-07-30 2020-11-03 浪潮云信息技术股份公司 Cloud platform virtual machine image automatic manufacturing, testing and publishing device and method based on DevOps

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
KHAOS: "Python学习:基于paramiko的交互式shell", pages 1 - 8, Retrieved from the Internet <URL:https://www.jianshu.com/p/d22d577386bc> *
TIANMAI: "recvuntil类的使用", pages 1 - 2, Retrieved from the Internet <URL:https://tianmai1.github.io/2021/04/14/recvuntil%E7%B1%BB%E7%9A%84%E4%BD%BF%E7%94%A8/> *
信斌;王桂花;: "基于Python和Jenkins的设备异常诊断系统设计与实现", 软件导刊, no. 11, 15 November 2017 (2017-11-15) *

Similar Documents

Publication Publication Date Title
JP3936835B2 (en) Terminal device for computer network and operation history recording method
US7559001B2 (en) Method and apparatus for executing commands and generation of automation scripts and test cases
US8458521B2 (en) Remote technical support employing a configurable executable application
WO2021042744A1 (en) File uploading method and system
CA2730823C (en) Multiple simultaneous session support by a remote technician
US10565302B2 (en) Method of organizing dialog with the use of fillable forms
CN109344058B (en) Automatic small program testing method
JP6436705B2 (en) Test execution device, test execution method, and computer program
CN113190296A (en) Paramiko-based interactive command execution method and device
JP2006277771A (en) Terminal device and operation history recording method for computer network
US11182131B2 (en) System and method that support production management
JP4293169B2 (en) Network equipment control system
CN113301050B (en) Multi-user real-time login and logout management method, system, terminal and medium for webpage
CN111708568B (en) Modularized development decoupling method and terminal
JP6436704B2 (en) Test execution device, test execution method, and computer program
KR100807055B1 (en) Automatically internet connection method using point-to-point protocol
CN111540133B (en) Method and device for detecting state of external equipment and related equipment
JP6353759B2 (en) Test execution device, test execution method, and computer program
CN109256124A (en) A kind of phonetic order creation method for user equipment end
CN112215587B (en) Method, device and system for automatically executing business process interaction tasks by multiple devices
CN113542827B (en) Multi-channel data transmission method and display device
WO2023230797A1 (en) Cross-system test method and apparatus
JPH1091235A (en) Network system having process management function
CN113067735A (en) Method, system and device for remotely configuring server network
CN117519524A (en) Terminal application testing method and device, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination