嗨,我想知道如何在类之间传递obeject。我将使用我的脚本首先登录到我们的服务器,客户端服务器旁边,而不是路由器和交换机。 下面是脚本: 文件名是connect.py:类之间的Python传递对象
import expect
class domain_connect:
def __init__(self,usr='user',pwd='password',d1='192.168.1.3',dclient='192.168.2.3'):
self._usr =us
self._pwd =pwd
self._d1 =d1
self._dclient =dclient
def djump(self):
child=pexpect.spawn('ssh -o StrictHostKeyChecking=no -l ' +self._usr+" "+self._d1)
child.expect('assword:')
child.sendline(self._pwd)
child.expect(':~>')
##Connect to client server
child.sendline(self._dclient)
child.expect('assword:')
child.sendline(self._pwd)
child.expect('accept:')
child.sendline(' ')
child.expect(':~>')
return child
class ce_connect:
def __init__(self, child, ip, usr, pwd, enpwd):
self._child = child
self._ip = ip
self._usr = usr
self._pwd = pwd
self._enpwd = pwd
def cjump(self):
##Connecting to router
child.sendline('ssh -o StrictHostKeyChecking=no -l '+self._usr+' '+self._ip)
return self._child
下面是使用脚本:
import connect
child = connect.domain_connect()
child = child.djump() ## At this point I'm good toward the client server
我尝试做以下儿童对象传递给ce_connect类
child2 = connect.ce_connect(child, '192.78.1.20', 'username', 'password', 'password2')
child2 = child2.cjump()
我得到一个错误:
AttributeError: 'spawn' object has no attribute 'djump'
请修复缩进。我想我可以猜到什么是什么,但我可能是错的,这可能会让我的答案错误。 (并且它阻止了其他人甚至试图回答。) – abarnert