-
一、安裝SSHLibrary
-
二.關鍵字
-
1.與連接相關的
- Open Connection
- Get Connection
- Get Connections
- Switch Connection
- Close Connection
- Close All Connections
- Login
- Login With Public Key
- Set Client Configuration
- Set Default Configuration
- Enable Ssh Logging
-
2.與文件/目錄相關的
- 2.1 File Should Exist , File Should Not Exist, Directory Should Exist , Directory Should Not Exist
- 2.2 List Directory, List Files In Directory , List Directories In Directory
- 2.3 Put Directory ,Get Directory,Put File,Get File
-
3.與讀寫執行相關的
- Write
- Write Bare
- Write Until Expected Output
- Read
- Read Until
- Read Until Prompt
- Read Until Regexp
- Execute Command
- Start Command
- Read Command Output
-
一、安裝SSHLibrary
二.關鍵字
1.與連接相關的
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)
更改默認設置:
1.導入庫時: Library SSHLibrary 10 seconds prompt=$
2.使用 Set Client Configuration/Set Default Configuration
3.調用Open Connection時:
結果:
${con1} =index=1 path_separator=/ prompt=# width=80 newline= height=24 encoding=UTF-8 alias=backend host=10.69.140.112 timeout=3 seconds term_type=vt100 port=2222
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字符串。
結果:
${con1} =index=1 path_separator=/ prompt=# width=80 newline= height=24 encoding=UTF-8 alias=backend host=10.69.140.112 timeout=3 seconds term_type=vt100 port=2222
${con2} = (2222, 3.0)
Get Connections
用法:沒有輸入值,獲取所有打開的connection的信息
結果:
${con1} = index=1 path_separator=/ prompt=$ width=80 newline= height=24 encoding=UTF-8 alias=None host=10.69.144.140 timeout=3 seconds term_type=vt100 port=2222
${con2} = index=2 path_separator=/ prompt=# width=80 newline= height=24 encoding=UTF-8 alias=None host=10.69.144.139 timeout=3 seconds term_type=vt100 port=2222
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。
結果:
20180418 17:11:20.688 : INFO : 10.0
20180418 17:11:20.689 : INFO : 5.0
20180418 17:11:20.689 : INFO : 3.0
Enable Ssh Logging
用法:[ logfile ],將SSH協議的日志輸出到本地給定的“日志文件”中。Enables logging of SSH protocol output to given `logfile`.
note:
1.文件存在時,文件內容被重寫。
2.沒有指定路徑時,該文件在RF文件夾中。
2.與文件/目錄相關的
2.1 File Should Exist , File Should Not Exist, Directory Should Exist , Directory Should Not Exist
用法:[ path ] ,無返回值
2.2 List Directory, List Files In Directory , List Directories In Directory
用法: [ path | pattern=None | absolute=False ],返回值為list。其中absolute=true ,返回絕對路徑。
結果:
20180417 14:38:58.281 : INFO : ${dir} = [u'cmd', u'service']
20180417 14:38:58.286 : INFO : ${dir2} = [u'/opt/ygomi/roadDB/jar/cmd', u'/opt/ygomi/roadDB/jar/service']
20180417 14:38:58.290 : INFO : ${dir3} = [u'service']
2.3 Put Directory ,Get Directory,Put File,Get File
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,將子目錄中文件也下載至本地上。

3.與讀寫執行相關的
Write
用法:[ text | loglevel=None ],將text寫入到遠端機器上並回車執行,返回值為text+換行符
loglevel:TRACE, DEBUG, INFO and WARN
Write Bare
用法:[ text ],將text寫入到遠端機器上,沒有執行沒有返回值。
結果:
關鍵字write 有返回值,執行命令后,用read讀取終端輸出時不會讀取到輸入的text;
關鍵字write bare沒有返回值,執行命令后,用read讀取終端輸出時會讀取到輸入的text;
Write Until Expected Output
用法:[ text | expected | timeout | retry_interval | loglevel=None ],沒有返回值,輸入的 text 在設定的timeout內每隔retry_interval重復執行,直到終端輸出的結果中包含期望的輸出expected。
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的返回值。
結果:
${var} = [u'/home/roaddb', u'', 0]
Other:
1.For executing commands on the remote machine, there are two possibilities:
- Execute Command and Start Command. The command is executed in a new shell on the remote machine, which means that possible changes to the environment (e.g. changing working directory, setting environment variables, etc.) are not visible to the subsequent keywords.
- Write, Write Bare, Write Until Expected Output, Read, Read Until, Read Until Prompt and Read Until Regexp. These keywords operate in an interactive shell, which means that changes to the environment are visible to the subsequent keywords.
2.SSHLibrary 官方文檔:http://robotframework.org/SSHLibrary/SSHLibrary.html#Importing