import getpass,select,socket,sys
import paramiko,logging
logging.basicConfig(level=logging.DEBUG, filename='bastion.log',
format='%(asctime)s - %(levelname)s: %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
user =raw_input('username:')
pwd =getpass.getpass('password')
dic ={'pi': ['192.168.1.105','c10.puppet.com','c11.puppet.com',],'eric': ['c100.puppet.com',]}
if user =='pi'and pwd =='123':
logging.info('登陆成功')
if dic.get(user,''):
print(dic.get(user,''))
NO=raw_input('选择ip对应NO:')
else:
print('你还没有访问资源,联系管理员添加')
sys.exit()
else:
logging.info('登陆失败')
sys.exit()
hostname ='192.168.1.105'
port =22
tran =paramiko.Transport((hostname, port,))
tran.start_client()
default_path='C:\Users\zhouzw\Downloads\id_rsa'
key =paramiko.RSAKey.from_private_key_file(default_path)
tran.auth_publickey('pi', key)
chan =tran.open_session()
chan.get_pty()
chan.invoke_shell()
def windows_shell(chan):
import threading
sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")
def writeall(sock):
while True:
data = sock.recv(256)
if not data:
logging.info(data)
sys.stdout.write('\r\n*** EOF ***\r\n\r\n')
sys.stdout.flush()
break
sys.stdout.write(data)
sys.stdout.flush()
writer = threading.Thread(target=writeall, args=(chan,))
writer.start()
try:
while True:
d = sys.stdin.read(1)
if not d:
break
print d
logging.info(d)
chan.send(d)
except EOFError:
pass
windows_shell(chan)
chan.close()
tran.close()