代码:
''' Author: 20004@163.com Date: 2022-08-01 11:34:45 LastEditTime: 2022-08-01 12:44:35 LastEditors: 20004@163.com Description: FilePath: \PythonCode\百度下拉词\网络技术\SSH\cisco\ssh3.py 可以输入预定的版权声明、个性签名、空行等 ''' import paramiko from getpass import getpass import time ''' description: param {*} hostname param {*} username param {*} password return {*} ''' def SSHServer(hostname,username,password): server=paramiko.client.SSHClient() server.set_missing_host_key_policy(paramiko.AutoAddPolicy()) server.connect(hostname, username=username,password=password) return server ''' description: param {*} sshclient param {*} commands param {*} end return {*} ''' def ssh_exec(sshclient, commands): try: print(f"\n{'#'*50}\nConnecting to the SSh Server:\n{'#'*50}") DEVICE_ACCESS = sshclient.invoke_shell() for command in commands: DEVICE_ACCESS.send(f'{command}\n') time.sleep(5) output = DEVICE_ACCESS.recv(65000) print (output.decode(), end='') sshclient.close() except: print("Unable to connect to the Device") cmd1 = ["cd /www/wwwroot/www.xxx.net/blog/", "wp core version","wp plugin list"] cmd2 = ["cd /www/wwwroot/www.xxx.net", "wp core version"] server1= SSHServer("xxx","xxx","xxx") ssh_exec(server1,cmd1)