利用Python第三方模块paramiko实现客户端管理工具
2022/5/1 20:12:56
本文主要是介绍利用Python第三方模块paramiko实现客户端管理工具,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import paramiko import optparse import sys def get_params(): parser = optparse.OptionParser('Usage: <Program> -t target -u username -p password') parser.add_option('-t', '--target', dest='target', type="string", help="Specify IP address of target") parser.add_option('-u', '--username', dest='username', type='string', help='Specify username to login SSH server') parser.add_option('-p', '--password', dest='password', type='string', help='Specify password to login SSH server') options, args = parser.parse_args() if options.target is None or options.username is None or options.password is None: print(parser.usage) sys.exit(0) return options.target, options.username, options.password class SSHClient: def __init__(self, server_ip, username, password) -> None: try: self.server_ip = server_ip self.username = username self.password = password self.ssh_client = paramiko.SSHClient() self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh_client.connect(hostname=self.server_ip, username=self.username, password=self.password) except paramiko.AuthenticationException: print("Failed to authenticate the SSH server") sys.exit(0) except: print("Failed to connect the SSH server") sys.exit(0) def run(self): while True: command = input("#~ ") if command == 'q': break stdin, stdout,stderr = self.ssh_client.exec_command(command) if stdout: res = stdout.read().decode('utf-8') print(res) if stderr: err_res = stderr.read().decode('utf-8') print(err_res) self.ssh_client.close() def banner(): banner = """ ****************************************************************** ****************************************************************** SSH Client by Jason Wong V1.0 ****************************************************************** ****************************************************************** """ print(banner) if __name__ == '__main__': banner() ssh_server_ip, username, password = get_params() ssh_client = SSHClient(ssh_server_ip, username, password) ssh_client.run()
运行效果图:
这篇关于利用Python第三方模块paramiko实现客户端管理工具的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门