Пример #1
0
def posix_shell(chan, port):
    import select

    oldtty = termios.tcgetattr(sys.stdin)

    s_in = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s_in.bind(('', port))

    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)

        while True:
            r, w, e = select.select([chan, sys.stdin, s_in], [], [])
            if chan in r:
                try:
                    x = u(chan.recv(1024))
                    if len(x) == 0:
                        s_in.close()
                        sys.stdout.write('\r\n*** EOF\r\n')
                        break
                    # encode msg in ascii
                    x = unicodedata.normalize('NFKD', x).encode('ascii', 'ignore')
                    sys.stdout.write(x)
                    sys.stdout.flush()

                    # send back
                    output_queue.push(x)

                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)
                if len(x) == 0:
                    break
                chan.send(x)
            if s_in in r:
                data, addr = s_in.recvfrom(1024)
                chan.send(data)
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
Пример #2
0
# paramiko.util.log_to_file('demo.log')

port = int(sys.argv[1])
hostname = 'localhost'
username = '******'
password = '******'
sshPort = 22


# now connect
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((hostname, sshPort))
except Exception as e:
    msg = '*** Connect failed: ' + str(e)
    output_queue.push(msg)
    print(msg)
    traceback.print_exc()
    sys.exit(1)

try:
    t = paramiko.Transport(sock)
    try:
        t.start_client()
    except paramiko.SSHException:
        msg = '*** SSH negotiation failed.'
        output_queue.push(msg)
        print(msg)
        sys.exit(1)

    try: