from paramiko.client import SSHClient, AutoAddPolicy
class SSHCmd(object):
def __init__(self,server1,password1):
self.server1 = server1
self.password1 = password1
self.ssh = SSHClient()
self.chan = None
self.connect()
def connect(self):#connect to the first One ( server1 )
self.ssh.set_missing_host_key_policy(AutoAddPolicy())
self.ssh.connect(self.server1, username='USERNME', password='PASS')
self.chan = self.ssh.invoke_shell()
def sendCMD(self,cmd,endswith):
if cmd[-1]!='\n':
cmd+='\n'
buff = ''
while not buff.endswith(endswith):
resp = self.chan.recv(9999)
buff += str(resp.decode())
print(buff)
self.chan.send(cmd)
return buff
def getBuffer(self,ends='> '):
buff = ''
while not buff.endswith(ends):
resp = self.chan.recv(9999)
buff+= resp.decode()
return buff
def close(self):
self.ssh.close()