利用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-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python