Python堡垒机

 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")
原文地址:https://www.cnblogs.com/Bruce-yin/p/7101778.html