跳板機(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這些參數,就可以連接到目標內網服務器上了。
上一篇c 寫入json