sshpipe提供工具来管理到远程主机的ssh通道。

sshpipe的Python项目详细描述


Overview

sshpipe was build as part of Eventor and Sequent to allow distributed processing using SSH. Usually, network based system are using ports to communicate between server and clients. However, in development environment, there may be many developers in need to individual port assignments. The management of such operation can become overwhelming.

With SSH tunneling, each developer can manage its own environment. Using virtualenvs and SSH keys, developer can manage himself connections between servers and clients they are working on.

If you have comments or insights, please don’t hesitate to contact us at support@acrisel.com

sshconfig

sshconfig is used to read SSH configuration file and give access to information stored there. It can be used also to save SSH configuration.

Loads and dumps

loads(), load() methods are used to read SSH configuration from string stream or file respectively into SSHConfig object. dumps(), dump() methods are used to write SSHConfig object to string stream or file respectively.

SSHConfig Class

SSHConfig class holds SSH configuration as read by load() or loads(). It can then be stored back into SSH configuration file with sshconfig’s dump() and dumps(). *SSHConfig provides get() method to retrieve SSH settings.

Future functionality:

  1. validation of configuration.
  2. manipulation of configuration (e.g., add key, change flags, etc.)

SSHPipe

SSHPipe class is used to initiate an SSH channel to a process running in remote host. SSHPipe is initiated with the command for the agent process. It would then start the agent (commonly an object of SSHPipeClient or of a class inheriting from it.)

Once agent is started, SSHPipe provides methods to send the agent work assignments. When agent is done or fails, it would communicate back to SSHPipe object. The method response() can be used to retrieve that response.

Example

This example shows a SSHPipe creation from one host to another with. The service in the remote host will accept string message sent via the pipe and would store them into a file.

sshremotehandlerusage.py, below, initiates

 1 importos 2 importmultiprocessingasmp 3 fromsshpipeimportSSHPipe 4  5 defrun(): 6 agent_dir='/var/acrisel/sand/acris/sshpipe/sshpipe/sshpipe_examples' 7 agentpy=os.path.join(agent_dir,"sshremotehandler.py") 8 host='ubuntud01_eventor'# SSH config host name of remote server. 9 10 sshagent=SSHPipe(host,agentpy)11 sshagent.start()12 13 ifnotsshagent.is_alive():14 print("Agent not alive",sshagent.response())15 exit(1)16 17 sshagent.send("This is life.\n")18 sshagent.send("This is also life.\n")19 sshagent.send("This is yet another life.\n")20 sshagent.send("That is all, life.\n")21 sshagent.send("TERM")22 23 ifnotsshagent.is_alive():24 print(sshagent.response())25 exit()26 27 response=sshagent.close()28 ifresponse:29 exitcode,stdout,stderr=response30 print('Response: ',response)31 32 if__name__=='__main__':33 mp.set_start_method('spawn')34 run()

The remote agent sshremotehandler.py is would run through SHHPipe and would loop awaiting input on its stdin stream.

 1 importlogging 2 fromsshpipeimportSSHPipeHandler 3  4 module_logger=logging.getLogger(__file__) 5  6 classMySSHPipeHandler(SSHPipeHandler): 7  8 def__init__(self,*args,**kwargs): 9 super(MySSHPipeHandler,self).__init__(*args,**kwargs)10 self.file=None11 12 defatstart(self,received):13 file="{}{}".format(__file__,".remote.log")14 self.module_logger.debug("Opening file: {}.".format(file))15 self.file=open(file,'w')16 17 defatexit(self,received):18 ifself.fileisnotNone:19 self.file.close()20 super(MySSHPipeHandler,self).atexit(received)21 22 defhandle(self,received):23 self.file.write(str(received))24 25 if__name__=='__main__':26 client=MySSHPipeHandler()27 client.service_loop()

The handler overrides the four methods of SSHPipeHandler. __init__() defines an instance member file, atstart() opens file to which records would be written, atexit() closes the file, and handle() writes received record to file.

Example Explanation

假设我们在某个服务器ubuntud20上运行sshremotehandlerusage.py程序

Classes and Methods

SSHPipe(host,command,name=None,user=None,term_message='TERM',config=None,encoding='utf8',callback=None,logger=None)SSHPipeestablishesconnectiontoremote*host*andruns*command*.*host*canbeipaddress,hostname,orSSHhostname.*name*associatesandidtothepipe.If*user*isprovided,itwillusefortheSSHconnectivity.term_message,is

SSHPipeClient

示例

importlogging# create loggerlogger=logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create console handler and set level to debugch=logging.TimedRotatingFileHandler()ch.setLevel(logging.DEBUG)# create formatterformatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')

MpLogger and LevelBasedFormatter

Multiprocessor logger using QueueListener and QueueHandler It uses TimedSizedRotatingHandler as its logging handler

It also uses acris provided LevelBasedFormatter which facilitate message formats based on record level. LevelBasedFormatter inherent from logging.Formatter and can be used as such in customized logging handlers.

example

在主进程中
importtimeimportrandomimportloggingfromacrisimportMpLoggerimportosimportmultiprocessingasmpdefsubproc(limit=1,logger_info=None):logger=MpLogger.get_logger(logger_info,name="acrilog.subproc",)foriinrange(limit):sleep_time=3/random.randint(1,10)time.sleep(sleep_time)logger.info("proc [%s]: %s/%s - sleep %4.4ssec"%(os.getpid(),i,limit,sleep_time))level_formats={logging.DEBUG:"[ %(asctime)s ][ %(levelname)s ][ %(message)s ][ %(module)s.%(funcName)s(%(lineno)d) ]",'default':"[ %(asctime)s ][ %(levelname)s ][ %(message)s ]",}mplogger=MpLogger(logging_level=logging.DEBUG,level_formats=level_formats,datefmt='%Y-%m-%d,%H:%M:%S.%f')logger=mplogger.start(name='main_process')logger.debug("starting sub processes")procs=list()forlimitin[1,1]:proc=mp.Process(target=subproc,args=(limit,mplogger.logger_info(),))procs.append(proc)proc.start()forprocinprocs:ifproc:proc.join()logger.debug("sub processes completed")mplogger.stop()

Example output

[2016-12-19,11:39:44.953189][DEBUG][startingsubprocesses][mplogger.<module>(45)][2016-12-19,11:39:45.258794][INFO][proc[932]:0/1-sleep0.3sec][2016-12-19,11:39:45.707914][INFO][proc[931]:0/1-sleep0.75sec][2016-12-19,11:39:45.710487][DEBUG][subprocessescompleted][mplogger.<module>(56)]

Clarification of parameters

name

name标识记录器的基名称。注意,此参数在mplogger init方法及其start方法中都可用。

当设置了consolidate时,mplogger init的name参数用于合并记录器。如果调用start()方法时未提供主进程的私有记录器,则它也用于主进程的私有记录器。

proecess_key

process_key定义一个或多个日志记录字段,该字段将成为日志文件名的一部分。如果使用它,记录器将为每个记录的进程密钥提供一个文件。如果设置了consolidate,这将是对合并日志的补充。

默认情况下,mplogger使用name作为进程键。如果提供了其他内容,例如processname,则它将作为后缀连接到name

file_prefix and file_suffix

允许通过设置file_prefixfile_suffix中的一个(或两个)来区分不同运行的日志集。通常,使用pid和粒度datetime作为前缀或后缀会创建一组唯一的日志。

file_mode

file_mode让程序定义如何打开日志。默认情况下,日志以追加模式打开。母鸡,历史被收集起来,并在一夜之间按大小归档。

consolidate

consolidate设置后,将从所有处理日志创建合并日志。 如果设置了consolidated,并且在没有name的情况下调用start(),则将合并到主进程中。

kwargs

kwargs是将传递给filehandler的命名参数。这包括:
文件模式='a',用于旋转文件处理程序
maxbytes=0,用于旋转文件处理程序
backupcount=0,用于rotatingfilehandler和timedrotingfilehandler
编码='ascii',用于旋转filehandler和timedrotingfilehandler
delay=false,对于timedrotatingfilehandler
when='h',用于timedrotatingfilehandler
间隔=1,timedrotatingfilehandler
utc=false,timedrotatingfilehandler
attime=none,对于timedrotatingfilehandler

Next Steps

  1. Acknowledgment from handler that SSH pipe was established.
  2. SSHMultiPipe to allow management of multiple pipe from a single point.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法在未设置数据源的情况下启动springboot应用程序   返回/泛型的类型?   java通过在navigationView中按id重新加载navigationView内容   java实现安卓的状态更新   java Equals()对于两个相等的字符串不返回true   java如何保存屏幕截图(matlab)   java GWT如何在重新加载页面之前确保函数已完全执行   java在Groovy中实现ObjectJSON映射的标准方法是什么?   java在ApacheTomcat中,是否可以通过连接器过滤多个访问日志文件?   java当JVM达到其Xmx限制时,它会强制垃圾收集吗?   如何在JAVA中生成包含特定数字的不同随机数列表?   rcp中透视图之间的java切换   java理解名为“分区”的Linkedlist算法中的无限循环   RestTemplate的java测微计统计信息   Android中使用自定义服务BLE的java读/写特性   java验证输入以确保负数   关于Java扫描器的io基本查询   java如何使用子字符串或其他函数将字符串拆分为单词?   java Storm群集重复元组