淘先锋技术网

首页 1 2 3 4 5 6 7

跳板机(JumpServer)是一种网络隔离技术,它通过一台服务器间接地访问其他内网服务器,保障内网服务器的安全。

而Python作为一种高级编程语言,可用于开发跳板机,其主要用途是为了方便管理员通过一台中转服务器访问内网服务器,同时对内网服务器进行控制。

import paramiko
class JumpServer:
def __init__(self, jump_server, ssh_user, ssh_key_path, destination_server, destination_user, destination_password):
self.jump_server = jump_server
self.ssh_user = ssh_user
self.ssh_key_path = ssh_key_path
self.destination_server = destination_server
self.destination_user = destination_user
self.destination_password = destination_password
self.port = 22
self.jump_server_port = 22
self.client = ''
def connect(self):
jump_server_client = paramiko.SSHClient()
jump_server_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jump_server_client.connect(hostname=self.jump_server, port=self.jump_server_port,
username=self.ssh_user, key_filename=self.ssh_key_path)
jump_command = 'ssh -L 22:{}:{} {}@{}'.format(self.destination_server, self.port,
self.destination_user, self.destination_server)
jump_server_client.exec_command(jump_command)
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname='127.0.0.1', port=self.port,username=self.destination_user,password=self.destination_password)
def execute(self, command):
stdin, stdout, stderr = self.client.exec_command(command)
print(stdout.read().decode())
def close(self):
self.client.close()
if __name__ == '__main__':
js = JumpServer('jumpserver_ip', 'ssh_user', 'ssh_key_path', 'destination_server_ip', 'destination_server_user', 'destination_password')
js.connect()
js.execute('ls')
js.close()

上面的代码通过paramiko模块实现了跳板机的连接与命令执行的功能,通过设置jump_server、ssh_user、ssh_key_path、destination_server、destination_user、destination_password这些参数,就可以连接到目标内网服务器上了。