/usr/share/doc/python-libssh2/examples/ssh.py is in python-libssh2 1.0.0-1.2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #!/usr/bin/env python
#
# pylibssh2 - python bindings for libssh2 library
#
# Copyright (C) 2010 Wallix Inc.
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import atexit
import select, socket, sys
import tty, termios
import libssh2
usage = """Do a SSH connection with username@hostname
Usage: ssh.py <hostname> <username> <password>"""
class MySSHClient:
def __init__(self, hostname, username, password, port=22):
self.hostname = hostname
self.username = username
self.password = password
self.port = port
self._prepare_sock()
def _prepare_sock(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.hostname, self.port))
self.sock.setblocking(1)
except Exception, e:
print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)
print e
try:
self.session = libssh2.Session()
self.session.set_banner()
self.session.startup(self.sock)
hash = self.session.hostkey_hash(2)
#print "----"
#import base64
#print base64.encodestring(hash)
#print "----"
# authentication
self.session.userauth_password(self.username, self.password)
except Exception, e:
print "SSHError: Can't startup session"
print e
def run(self):
try:
# open channel
channel = self.session.open_session()
# request X11 Forwarding on display 0
channel.x11_req(0)
# request pty
channel.pty('vt100')
# request shell
channel.shell()
channel.setblocking(0)
# loop
while True:
data_to_disp = channel.poll(0, 1)
if data_to_disp > 0:
data = channel.read(1024)
if data is not None:
sys.stdout.write(data)
else:
break
sys.stdout.flush()
r,w,x = select.select([fd],[],[],0.01)
if sys.stdin.fileno() in r:
data = sys.stdin.read(1).replace('\n','\r\n')
channel.write(data)
except Exception,e:
print e
finally:
channel.close()
def __del__(self):
self.session.close()
self.sock.close()
if __name__ == '__main__' :
if len(sys.argv) == 1:
print usage
sys.exit(1)
# save terminal settings
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
# enable raw mode
tty.setraw(fd)
myssh = MySSHClient(sys.argv[1],sys.argv[2], sys.argv[3])
myssh.run()
# restore terminal settings
atexit.register(
termios.tcsetattr,
sys.stdin.fileno(), termios.TCSADRAIN, old_settings
)
|