5.遠程服務器操作(SSHLibrary)


一、SSHLibrary

robotframework做自動化測試,在流程中可能需要遠程連接機器做一些簡單操作,比如連接linux服務器,平時測試的時候使用客戶端工具去連接就是遠程連接。
遠程連接有兩種:
一種是用戶名密碼登錄連接;
一種是通過密鑰連接:比如工具輸入用戶名、密碼去連接服務器,再比如連接git很多都是用密鑰。
在robotframework中要遠程連接可以使用SSHLibrary庫來做操作。

1、安裝並引入庫

pip install robotframework-SSHLibrary

2、關鍵字介紹

Open Connection
用法: [ host | alias=None | port=22 | timeout=None | newline=None | prompt=None | term_type=None | width=None | height=None | path_separator=None | encoding=None ]
默認設置:timeout=3 seconds, newline=LF, prompt=None, loglevel=INFO, term_type=vt100, width=80,height=24, path_separator=/, encoding=UTF-8.其中,newline=LF/CRLF(\n,\r\n)

Get Connection
用法:[ index_or_alias=None | index=False | host=False | alias=False | port=False | timeout=False | newline=False | prompt=False | term_type=False | width=False | height=False | encoding=False ]
1.獲取connection的信息,如果調用時沒有加 index_or_alias,返回當前的conection信息。
2.獲取connection的特定屬性信息,后邊加屬性名=非false/False字符串。

Get Connections

用法:沒有輸入值,獲取所有打開的connection的信息

Switch  Connection

用法:[ index_or_alias ],跳轉到另一個active的connection。

Close Connection

用法:沒有輸入值,關閉當前的connection

Close All Connections

用法:沒有輸入值,關閉所有打開的connection

Login

用法:[ username | password | delay=0.5 seconds ]

 

Login With Public Key
用法:[ username | keyfile | password= | delay=0.5 seconds ]
Set Client Configuration
用法:[ timeout=None | newline=None | prompt=None | term_type=None | width=None | height=None | path_separator=None | encoding=None ],設置當前connection的配置
Set Default  Configuration
用法:[ timeout=None | newline=None | prompt=None | term_type=None | width=None | height=None | path_separator=None | encoding=None ],設置默認的配置,但不影響已經open的connection。

Enable Ssh Logging
用法:[ logfile ],將SSH協議的日志輸出到本地給定的“日志文件”中。Enables logging of SSH protocol output to given `logfile`. 
note:
    1.文件存在時,文件內容被重寫。
    2.沒有指定路徑時,該文件在RF文件夾中。

與文件/目錄相關的
File Should Exist ,     
File Should Not Exist,     
Directory Should Exist ,   
Directory Should Not Exist
用法:[ path ] ,無返回值
List Directory,   
List Files In Directory ,   
List Directories In Directory
用法: [ path | pattern=None | absolute=False ],返回值為list。其中absolute=true ,返回絕對路徑。

Put File
用法:[ source | destination=. | mode=0744 | newline= ],無返回值,將文件從本地上傳到遠程機器上。
`newline` can be used to force the line break characters that are written to the remote files. Valid values are `LF` and `CRLF`. 
Put Directory
用法:[ source | destination=. | mode=0744 | newline= | recursive=False ],將目錄由本地上傳到遠程機器上。如果recursive=True,將子目錄中文件也上傳至遠程機器上。
Get File
用法:[ source | destination=. ],從遠程機器中下載文件
Get Directory
用法:[ source | destination=. | recursive=False ],從遠程機器中下載目錄。如果recursive=True,將子目錄中文件也下載至本地上。

與讀寫執行相關的
Write
用法:[ text | loglevel=None ],將text寫入到遠端機器上並回車執行,返回值為text+換行符
loglevel:TRACE, DEBUG, INFO and WARN
Write Bare
用法:[ text ],將text寫入到遠端機器上,沒有執行沒有返回值。

Write Until Expected Output
用法:[ text | expected | timeout | retry_interval | loglevel=None ],沒有返回值,輸入的 text 在設定的timeout內每隔retry_interval重復執行,直到終端輸出的結果中包含期望的輸出expected。

關鍵字write 有返回值,執行命令后,用read讀取終端輸出時不會讀取到輸入的text; 關鍵字write bare沒有返回值,執行命令后,用read讀取終端輸出時會讀取到輸入的text;

Read
用法: [ loglevel=None | delay=None ] ,讀取終端輸出
Read Until
用法:[ expected | loglevel=None ]
Read Until Prompt
用法:[ loglevel=None ]

Read Until Regexp
用法:[ regexp | loglevel=None ]

Execute Command
用法:[ command | return_stdout=True | return_stderr=False | return_rc=False ]
在遠端機器上執行命令,並返回執行結果,需要等待command執行完才返回結果。
Start Command
用法:[command]
沒有返回值,不等待command執行完成就返回。
Read Command Output
用法:[ return_stdout=True | return_stderr=False | return_rc=False ]
與Start Command配合使用,執行Read Command Output前至少要先執行一次Start Command關鍵字,返回最近一次start command的返回值。

 

 

 

SSHLibrary 官方文檔:

http://robotframework.org/SSHLibrary/SSHLibrary.html#Importing

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM