嗨,我想知道如何在類之間傳遞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