# connect to client
client.connect('192.168.254.141',22,'test','test',allow_agent=False,look_for_keys=False)
# get shell
ssh_shell = client.invoke_shell()
# ready when line endswith '>' or other character
while True:
line = ssh_shell.recv(1024)
#print line
if line and line.endswith('>'):
break;
# send command
ssh_shell.sendall( 'ping 192.168.254.142' + '\n')
# get result lines
lines = []
while True:
line = ssh_shell.recv(1024)
if line and line.endswith('>'):
break;
lines.append(line)
result = ''.join(lines)
# print result
print result
# encoding=utf-8import paramikoimport timeclient = paramiko.SSHClient()client.load_system_host_keys()# connect to clientclient.connect('192.168.254.141',22,'test','test',allow_agent=False,look_...
ssh
_client = paramiko.
SSH
Client()
# 自动处理第一次
连接
的yes或者no的问题
ssh
_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
# port = '22'
# usernam.
import paramiko #导入paramiko包
client = paramiko.
SSH
Client() #创建
ssh
对象
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #首次
连接
设备时会询问yes or no,自动选择yes
client.load_system_host_keys() #加载之前的hosts...
python
脚本
连接
交换机
,自动
执行
命令
。
python
脚本
连接
交换机
,自动
执行
命令
,查询
交换机
端口的学习mac地址,自动写入txt中。设置定时器,每xx秒
执行
一次,自动对比上次返回结果,TXT中保存比较全的一次查询结果。
本人已经在ensp模拟器中测试成功了,文章最后附上拓扑图。
import paramiko
import time
import sys
import threading
def MAC1():
# 创建
SSH
对象
ssh
= paramiko.
SSH
Client(
# 定义
SSH
连接
参数
ssh
= paramiko.
SSH
Client()
ssh
.set_missing_host_key_policy(paramiko.AutoAddPolicy())
username = "your_username"
password = "your_password"
# 定义
交换机
列表,包括 IP 和端口号
switches = [
{"ip": "10.0.0.1", "port": 22},
{"ip": "10.0.0.2", "port": 22},
{"ip": "10.0.0.3", "port": 22}
# 循环
连接
每个
交换机
,并
执行
命令
for switch in switches:
ssh
.connect(switch["ip"], port=switch["port"], username=username, password=password)
stdin, stdout, stderr =
ssh
.exec_command("show version")
print(stdout.read().decode())
ssh
.close()
except:
print("Failed to connect to switch: " + switch["ip"])
这个示例代码
连接
了三个
交换机
,使用
SSH
连接
执行
了 `show version`
命令
,并打印
输出
结果。你可以根据自己的需求修改此代码。
木糖存香_Manny:
最小编辑距离python实现
片刻小哥哥:
最小编辑距离python实现
一刀不二: