cup.shell package

Submodules

cup.shell.expect module

author:Guannan Ma
create_date:2013
last_date:2014
descrition:Guannan just made a wraper out of pexpect. The original copyright belongs to the author of pexpect module. See it at http://pexpect.sourceforge.net/pexpect.html
cup.shell.expect.checkssh(hostname, username, passwd)[source]

判断ssh的连通性, 成功的话返回True, 否则False

cup.shell.expect.dscp(hostname, username, passwd, src, dst, timeout=9000, b_print_stdout=False)[source]

拷贝hostname, src目录到本地的dst目录。 采取scp -r的模式。

Returns:dict.有 ‘exitstatus’ ‘remote_exitstatus’ ‘result’ 三项输出内容
cup.shell.expect.go(hostname, username, passwd, command='', timeout=800, b_print_stdout=True)[source]

回返在hostname机器上执行的shell命令结果信息 历史兼容函数。 推荐使用go_ex command 有较多转义字符的,不推荐使用go,推荐使用go_witch_scp

Returns:类型tuple: (ret, return_string) ret 是本机执行远程命令的返回值 return_string 是远程执行这条shell的输出值
cup.shell.expect.go_ex(hostname, username, passwd, command='', timeout=800, b_print_stdout=True)[source]

回返在hostname机器上执行的shell命令结果信息, 相比go而言, 回返信息更丰富,且按照dict方式回返. command 有较多转义字符的,不推荐使用go,推荐使用go_witch_scp

Returns:dict.有 ‘exitstatus’ ‘remote_exitstatus’ ‘result’ 三项输出内容
cup.shell.expect.go_witch_scp(hostname, username, passwd, command='', host_tmp='/tmp/', remote_tmp='/tmp/', timeout=8000, b_print_stdout=True)[source]

回返在hostname机器上执行的shell命令结果信息, 方式是:把command 写入本机文件,然后scp到远端机器,然后到远端执行此文件 相比go_ex 和 go 而言,多了如下参数

Parameters:
  • host_tmp – 本次保存command内容的文件目录
  • remote_tmp – 远端保持command内容的文件目录
Returns:

dict.有 ‘exitstatus’ ‘remote_exitstatus’ ‘result’ 三项输出内容

cup.shell.expect.lscp(src, hostname, username, passwd, dst, timeout=800, b_print_stdout=True)[source]

拷贝src到hostname的dst目录下。

Returns:dict.有 ‘exitstatus’ ‘remote_exitstatus’ ‘result’ 三项输出内容
cup.shell.expect.lscp_prod(scpstr, passwd, dst_path, timeout=800, b_print_stdout=True)[source]

兼容过去版本函数, 不推荐使用。

Module contents

author:Guannan Ma
create_date:2014
last_date:2014
descrition:shell related module
cup.shell.md5file(filename)[source]

计算一个文件的md5值。返回32位长的hex字符串。

cup.shell.kill9_byname(strname)[source]

kill -9 process by name

cup.shell.del_if_exist(path)[source]

如果文件/目录/symlink存在则删除他们

cup.shell.execshell(cmd, b_printcmd=True)[source]

执行shell命令,返回returncode

cup.shell.execshell_withpipe(cmd)[source]

以popen的方式执行某条shell命令, 返回os.popen(cmd) Deprecated. 不推荐使用, 推荐使用ShellExec

cup.shell.execshell_withpipe_exwitherr(cmd, b_printcmd=True)[source]

历史兼容函数。 不推荐使用 同execshell_withpipe_ex, 但回返信息是string. (将所有行join成一个string回返) 该函数会将stdout和stderr一同回返。

cup.shell.is_proc_alive(procname, is_whole_word=False)[source]

通过ps -ef|grep -w procname$ |grep -v grep|wc -l 判断进程是否存在 相关函数有: cup.oper.is_proc_exist(path, name)

cup.shell.forkexe_shell(cmd)[source]

fock一个进程并在该进程执行shell的cmd命令

cup.shell.execshell_withpipe_ex(cmd, b_printcmd=True)[source]

历史兼容函数。 执行某条shell命令,回返执行stdout的结果行(list). Deprecated. 不推荐使用, 推荐使用ShellExec.

cup.shell.execshell_withpipe_str(cmd, b_printcmd=True)[source]

历史兼容函数。 同execshell_withpipe_ex, 但回返信息是string. (将所有行join成一个string回返)

class cup.shell.ShellExec[source]

Bases: object

用来执行shell的类。 用法如下: shellexec = cup.shell.ShellExec() # timeout=None, 一直等待直到命令执行完 shellexec.run(‘/bin/ls’, timeout=None) # timeout>=0, 等待固定时间,如超时未结束terminate这个shell命令。 shellexec.run(cmd=’/bin/ls’, timeout=100)

run(cmd, timeout)[source]

参见类说明。

Parameters:
  • cmd – 执行命令
  • timeout – 执行等待时间, None为无线等待。 timeout>=0等待具体时间,超时 terminate.
Returns:

一个dict, 包含’stdout’ ‘stderr’ ‘returncode’ 三个key:

returncode == 0 代表执行成功, returncode 999代表执行超时

{

‘stdout’ : ‘Success’, ‘stderr’ : None, ‘returncode’ : 0

}

E.g.

执行ls, 超时时间为1s, 超过1s会kill掉该shell进程, 然后回返returncode 999

import cup
shelltool = cup.shell.ShellExec()
print shelltool.run('/bin/ls', timeout=1)

Table Of Contents

Previous topic

cup.services package

Next topic

cup.thirdp package

This Page