Demo entry 6631652

pip

   

Submitted by anonymous on Jul 18, 2017 at 10:36
Language: Python. Code size: 973 Bytes.

class Linux:

    def __init__(self):
        self.ip = "某IP地址"
        self.username = "用户名"
        self.password = "密码"
        self.timeout = "会话超时时间"
        self.try_times = "链接重试次数"
        self.t = ""
        self.chan = ""

    def connect(self):
        while True:
            try:
                self.t = paramiko.Transport(sock=(self.ip, 22))			#设置ssh链接的远程主机地址与端口号
                self.t.connect(username=self.username, password=self.password)			#设置登录名和密码
                self.chan = self.t.open_session()			#链接成功后,打开一个channel
                self.chan.settimeout(self.timeout)			#设置会话超时时间
                self.chan.get_pty()			#打开terminal
                self.chan.invoke_shell()			#激活terminal
                return			#连接成功,返回Linux对象
            except Exception, e:
                 if self.try_times != 0:
                     self.try_times -= 1			#本次连接失败,重试次数减1
                 else:
                     exit(1)			#连接尝试全部失败,退出

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).