Example #1
0
 def wrapper(ip, sw_id):
     password = secure.pass_chooser(sw_id)
     try:
         tn = telnetlib.Telnet(ip, timeout=TIMEOUT)
         func(ip, password, tn)
     except Exception as e:
         print('Error while rebooting {}: {}'.format(ip, e))
Example #2
0
    def wrapper(ip, sw_id, password=None, DEBUG=False):
        '''(ipaddress, switch_id, password, DEBUG) -> function
       
        Function wrapper for rebooting switches via ssh. 
        SNR-S2940-8G, SNR-S2940-24G and SNR-S2940-48G.

        '''

        # Create and connect a new socket.
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(3)
        try:
            sock.connect((ip, 22))
        except Exception as e:
            print('Error while connecting to {}, {}'.format(ip, e)) 
            sys.exit(1)
        TTY = os.isatty(sys.stdin.fileno())
                 
        # Create a new SSH session using that socket and login
        # using basic password authentication.
        print('Connecting to {}...'.format(ip))
        if not password:
            password = secure.pass_chooser(sw_id)
        user = secure.user
        if DEBUG:
            print('Using username', user)
            print('Using password', password)
        session = libssh2.Session()
        session.startup(sock)
        session.userauth_password(user, password)
         
        # Put the session into non-blocking mode.
        channel = session.channel()
        channel.request_pty('vt100')
        channel.shell()
        session.blocking = False

        if DEBUG:
            print('Getting attrs from stdin')

        if TTY:
            attrs = termios.tcgetattr(sys.stdin)
        else:
            attrs = sys.stdin.readline().rstrip()
        try:
            # Put terminal attached to stdin into raw mode.
            if TTY:
                tty.setraw(sys.stdin)
            try:
                # session.blocking = True
                reboot_function(channel)
            except Exception as e:
                print('\n {} reported\n'.format(ip, e))
        finally:
            # Restore attributes of terminal attached to stdin.
            sock.close()
            if TTY:
                termios.tcsetattr(sys.stdin, termios.TCSADRAIN, attrs)