1. 程式人生 > >Python堡壘機

Python堡壘機

sel delattr ssh read con tran policy center odin

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # import paramiko
 4 # msg = """
 5 #              \(≧▽≦)/歡迎來到地表最強跳板機\(≧▽≦)/
 6 # """
 7 # print(msg)
 8 # # 建立一個sshclient對象
 9 # ssh = paramiko.SSHClient()
10 # # 允許將信任的主機自動加入到host_allow 列表,此方法必須放在connect方法的前面
11 # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
12 # # 調用connect方法連接服務器 13 # ssh.connect(hostname=‘192.168.92.172‘,port=22,username=‘yinqiuliang‘,password=‘123.com‘) 14 # # 執行命令 15 # 16 # while True: 17 # input_chose = input("請輸入對服務器的操作命令:") 18 # stdin, stdout, stderr = ssh.exec_command(input_chose) 19 # # 結果放到stdout中,如果有錯誤將放到stderr中 20 #
print(stdout.read().decode()) 21 # 22 # ssh.close() 23 import paramiko 24 msg = """ 25 \(≧▽≦)/歡迎來到地表最強跳板機\(≧▽≦)/ 26 """ 27 print(msg) 28 # t = paramiko.Transport(("192.168.92.172","22")) 29 # t.connect(username="yinqiuliang",password="123.com") 30 #ssh = paramiko.SSHClient() 31 # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
32 # ssh.connect("192.168.92.172",22,"yinqiuliang", "123.com") 33 # stdin, stdout, stderr = ssh.exec_command("df -h") 34 # print(stdout.read().decode()) 35 # ssh.close() 36 # class server(host): 37 # def __delattr__(self, host,user,pwd): 38 # self.host = host 39 # self.user = user 40 # self.pwd = pwd 41 42 def server(host,Port,user,pwd): 43 ssh = paramiko.SSHClient() 44 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 45 ssh.connect(host, Port, user, pwd,timeout=30) 46 while True: 47 res_choise = input("請輸入對服務器的操作命令:") 48 #print(res) 49 if res_choise == exit: 50 exit() 51 elif len(res_choise) == 0: 52 continue 53 54 #stdin, stdout, stderr = ssh.exec_command(res_choise) 55 stdin, stdout, stderr = ssh.exec_command(res_choise) 56 res, err = stdout.read(),stderr.read() 57 resuilt = res if res else err 58 print("[%s]".center(50,"-") %host) 59 print(resuilt.decode()) 60 #print(stdout.read().decode()) 61 # print(stdin.read().decode()) 62 # print(stderr.read().decode()) 63 ssh.close() 64 # def command(): 65 # res = input("請輸入對服務器的操作命令:") 66 # #print(res) 67 # return res 68 69 if __name__ == __main__: 70 #command(‘df -h‘) 71 server("192.168.92.172",22,"yinqiuliang","123.com")

Python堡壘機