import traceback
import paramiko
import telnetlib
import time
from paramiko.ssh_exception import SSHException
class SSHClient():
def __init__(self,host_ip,username,password):
try:
self.ssh_client = paramiko.Transport(host_ip, 22)
self.ssh_client.connect(username=username, password=password)
self.ssh_channal = self.ssh_client.open_session()
self.ssh_channal.settimeout(300)
self.ssh_channal.get_pty()
self.ssh_channal.invoke_shell()
strs = str(self.ssh_channal.recv(65535), encoding='gb2312')
tmpstr = strs
while not strs.endswith('>'):
if tmpstr.endswith('characters.'):
self.ssh_channal.send('\n')
if tmpstr.endswith('[Y/N]:'):
self.ssh_channal.send('n\n')
strs = str(self.ssh_channal.recv(65535), encoding='gb2312')
tmpstr = tmpstr + strs
tmppos = tmpstr.rfind('\n')
self.name_read = tmpstr[tmppos + 1:]
except SSHException:
self.ssh_client=None
class TelnetClient():
def __init__(self,host_ip,username,password):
try:
self.tn = telnetlib.Telnet(host_ip, port=23, timeout=10)
name=self.tn.read_until(b'name: ',5)
self.tn.write(username.encode('utf-8') + b"\n")
self.tn.read_until(b'word: ',5)
self.tn.write(password.encode('utf-8') + b'\n')
prompt=self.tn.read_until(b'#',5)
self.tn.write(b' ' + b'\n')
except:
self.tn = None