HuaWei_Paramiko_Ssh
Posted 张贺贺
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HuaWei_Paramiko_Ssh相关的知识,希望对你有一定的参考价值。
HuaWei_Paramiko_Ssh
关于Paramiko的基本介绍在这个文档里面:点我跳转
第一版
要求:传递一个命令并取回执行结果
import time
import paramiko
port_number = 22
username = "zhangsan"
password = "passwordA1!"
ip = "192.168.80.101"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
port=port_number,
username=username,
password=password,
hostname=ip
)
ssh_cmd = ssh.invoke_shell()
time.sleep(1)
tmp99 = ssh_cmd.recv(65535)
ssh_cmd.send("dis clock\\n")
time.sleep(1)
tmp1 = ssh_cmd.recv(65535)
print(tmp1.decode())
"C:\\Program Files\\Python311\\python.exe" G:\\python2\\第十章\\text_files\\test22.py
dis clock
2023-03-22 19:39:33
Wednesday
Time Zone(DefaultZoneName) : UTC
<HUAWEI>
第二版
要求:传递多个命令并取回执行结果
# 前面的代码不用变,非常简单,连写三条命令即可
tmp99 = ssh_cmd.recv(65535)
ssh_cmd.send("dis clock\\n")
ssh_cmd.send("dis version\\n")
ssh_cmd.send("dis ip interface brie\\n")
time.sleep(1)
tmp1 = ssh_cmd.recv(65535)
print(tmp1.decode())
第三版
要求:用函数的方式写成代码块,这样连续执行多个命令
import time
import paramiko
def ssh(cmd):
port_number = 22
username = "zhangsan"
password = "passwordA1!"
ip = "192.168.80.101"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
port=port_number,
username=username,
password=password,
hostname=ip
)
ssh_cmd = ssh.invoke_shell()
time.sleep(1)
tmp99 = ssh_cmd.recv(65535)
ssh_cmd.send(cmd)
time.sleep(1)
tmp1 = ssh_cmd.recv(65535)
print(tmp1.decode())
cmds = ["dis version","dis ip int brie"]
for cmd in cmds:
cmd = f\'cmd\\n\'
ssh(cmd)
第四版
要求:要求把过于的信息过滤掉,只看自己想要的信息,比如说在不改变发送命令的前提下,只显示出具体的版本号和g1/0/1的接口信息;
# 初始版
import time
import paramiko
def ssh(cmd):
port_number = 22
username = "zhangsan"
password = "passwordA1!"
ip = "192.168.80.101"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
port=port_number,
username=username,
password=password,
hostname=ip
)
ssh_cmd = ssh.invoke_shell()
time.sleep(1)
tmp99 = ssh_cmd.recv(65535)
ssh_cmd.send(cmd)
time.sleep(1)
tmp1 = ssh_cmd.recv(65535)
tmp2 = f\'tmp1.decode()\'
tmp3 = tmp2.split("\\n")
print(tmp3[2])
cmds = ["dis version"]
for cmd in cmds:
cmd = f\'cmd\\n\'
ssh(cmd)
"C:\\Program Files\\Python311\\python.exe" G:\\python2\\第十章\\text_files\\test22.py
VRP (R) software, Version 8.180 (CE12800 V200R005C10SPC607B607)
但初始版非常不合理,因为只传递了一个命令,我们把命令的返回值搞成了列表,然后只打印我们需要的那一行,但我们我们传递的是多条命令,那想要的数据不一定在第二个元素,我们应该这样,在函数里面不动,还是打印那么多东西,但我们在向里面传统命令的时候我们再取我们想要的东西,这样就合理了,函数也通用了。
import time
import paramiko
def ssh(cmd):
port_number = 22
username = "zhangsan"
password = "passwordA1!"
ip = "192.168.80.101"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
port=port_number,
username=username,
password=password,
hostname=ip
)
ssh_cmd = ssh.invoke_shell()
time.sleep(1)
tmp99 = ssh_cmd.recv(65535)
ssh_cmd.send(cmd)
time.sleep(1)
tmp1 = ssh_cmd.recv(65535)
return tmp1 # 从这向下变了
version_1 = ssh("dis version \\n")
version_2 = version_1.decode()
version_3 = version_2.split("\\n")
print(version_3[2])
clock_1= ssh("dis clock\\n")
clock_2 = clock_1.decode()
clock_3 = clock_2.split("\\n")
print(clock_3[1])
"C:\\Program Files\\Python311\\python.exe" G:\\python2\\第十章\\text_files\\test22.py
VRP (R) software, Version 8.180 (CE12800 V200R005C10SPC607B607)
2023-03-22 20:16:18
第五版
在第四版我们使用ssh()函数时,发送的命令后面总得带着换行符,我们可以在函数当中让他自动添加;
def ssh(cmd):
cmd = f\'cmd\\n\'
……
第六版
version_1 = ssh("dis version \\n")
version_2 = version_1.decode()
version_3 = version_2.split("\\n")
print(version_3[2])
clock_1= ssh("dis clock\\n")
clock_2 = clock_1.decode()
clock_3 = clock_2.split("\\n")
print(clock_3[1])
上述是发送两个命令,并调用函数的,看着行数也有点多呀,你会发现前三行是一样的,我们可以再写一个函数,如下所示,这样代码就只有两行
import time
import paramiko
def ssh(cmd):
cmd = f\'cmd\\n\'
port_number = 22
username = "zhangsan"
password = "passwordA1!"
ip = "192.168.80.101"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
port=port_number,
username=username,
password=password,
hostname=ip
)
ssh_cmd = ssh.invoke_shell()
time.sleep(1)
tmp99 = ssh_cmd.recv(65535)
ssh_cmd.send(cmd)
time.sleep(1)
tmp1 = ssh_cmd.recv(65535)
return tmp1
def main(command):
a1 = ssh(command)
a2 = a1.decode()
a3= a2.split("\\n")
return a3
version1 = main("dis version")
print(version1[2])
colock = main("dis clock")
print(colock[1])
"C:\\Program Files\\Python311\\python.exe" G:\\python2\\第十章\\text_files\\test22.py
VRP (R) software, Version 8.180 (CE12800 V200R005C10SPC607B607)
2023-03-22 20:46:37
初学菜鸟,请勿见笑,以后续续优化……
以上是关于HuaWei_Paramiko_Ssh的主要内容,如果未能解决你的问题,请参考以下文章