def init(hostname, username, keyfile, passwd):
global sftp, tc
t = paramiko.Transport(hostname)
tc = t
try:
key = paramiko.RSAKey.from_private_key_file(keyfile, passwd)
except paramiko.PasswordRequiredException:
sys.stderr.write('\n\nparamiko.RSAKey.from_private_key_file REQUIRES PASSWORD.\n')
sys.stderr.write('You have two options:\n')
sys.stderr.write('* Use the "-K" option to point to a different (non-password-protected)\n')
sys.stderr.write(' private key file.\n')
sys.stderr.write('* Use the "-P" option to provide the password needed to unlock this private\n')
sys.stderr.write(' key.\n')
sys.stderr.write('\n')
sys.exit(1)
try:
t.connect(username=username, pkey=key)
except paramiko.SSHException:
t.close()
sys.stderr.write('\n\nparamiko.Transport.connect FAILED.\n')
sys.stderr.write('There are several possible reasons why it might fail so quickly:\n\n')
sys.stderr.write('* The host to connect to (%s) is not a valid SSH server.\n' % hostname)
sys.stderr.write(' (Use the "-H" option to change the host.)\n')
sys.stderr.write('* The username to auth as (%s) is invalid.\n' % username)
sys.stderr.write(' (Use the "-U" option to change the username.)\n')
sys.stderr.write('* The private key given (%s) is not accepted by the server.\n' % keyfile)
sys.stderr.write(' (Use the "-K" option to provide a different key file.)\n')
sys.stderr.write('\n')
sys.exit(1)
sftp = paramiko.SFTP.from_transport(t)
评论列表
文章目录