当前位置:网站首页>telnet+ftp 对设备进行 操控 和 升级
telnet+ftp 对设备进行 操控 和 升级
2022-06-29 15:44:00 【洪大宇】
import telnetlib
import time
from ftplib import FTP
class TelnetClient():
def __init__(self,host_ip,username,password):
self.tn = telnetlib.Telnet()
self.host_ip = host_ip
self.username = username
self.password = password
def login_host(self):
try:
self.tn.open(self.host_ip,port=23)
except:
print('%s网络连接失败'%self.host_ip)
return False
self.tn.read_until(b'login: ',timeout=10)
self.tn.write(self.username.encode('ascii') + b'\n')
self.tn.read_until(b'Password: ',timeout=10)
self.tn.write(self.password.encode('ascii') + b'\n')
command_result = self.tn.read_very_eager().decode('ascii')
if 'Login incorrect' not in command_result:
print('%s登录成功'%self.host_ip)
return True
else:
print('%s登录失败,用户名或密码错误'%self.host_ip)
return False
def execute_some_command(self,command):
self.tn.write(command.encode('ascii')+b'\n')
time.sleep(5)
print("\n***************************************************\n")
command_result = self.tn.read_very_eager().decode('ascii')
print('命令执行结果:\n%s' % command_result)
print("\n***************************************************\n")
def __del__(self):
self.tn.write(b"exit\n")
def TelnetExecCmd(tn:TelnetClient,cmd:str) -> bool:
if tn.login_host():
tn.execute_some_command(cmd)
tn.logout_host()
return True
else:
return False
class FtpClient():
def __init__(self,ip:str,username:str,password:str) -> None:
self.ip = ip
self.user = username
self.password = password
self.ftp = FTP()
def connect(self):
try:
self.ftp.set_debuglevel(2)
self.ftp.connect(self.ip,port=21)
self.ftp.login(self.user,self.password)
print(self.ftp.getwelcome())
except:
print("ftp connect {} error ...".format(self.ip))
def put(self,filename:str):
try:
fp = open(filename,"rb")
self.ftp.storbinary("STOR "+filename,fp)
except:
print("ftp upload {} error".format(filename))
def get(self,filename:str):
try:
fp = open(filename,"wb")
self.ftp.retrbinary("RETR "+filename,fp.write)
except:
print("ftp get {} error".format(filename))
def __del__(self):
self.ftp.set_debuglevel(0)
self.ftp.quit()
# AU DEVICE: 192.168.1.2
# RRU DEVICE: 192.168.98.98
if __name__ == '__main__':
host_ip = '192.168.1.2'
username = 'root'
password = 'root'
command = 'date'
# telnet_client = TelnetClient(host_ip,username,password)
# if TelnetExecCmd(telnet_client,command):
# print("Exec Successsful")
# else:
# print("Exec Failed")
ftp = FtpClient(host_ip,username,password)
ftp.connect()
ftp.put("Image")
time.sleep(3)
ftp.get("hello_debg.tar.xz")
边栏推荐
- Neural network for remote sensing image processing
- 【云原生】Nacos-TaskManager 任务管理的使用
- 华为云AOM 2.0版本发布
- 2022-06-29日报: 李飞飞划重点的「具身智能」,走到哪一步了?
- C. Most Similar Words
- Leetcode-470- implement rand10() with rand7()
- 按键精灵打怪学习-多窗口多线程后台判断人物、宠物血量和宠物快乐度
- 华为云AOM 2.0版本发布
- 发明了杀毒软件之后,他选择做一个极品混混
- The way of enterprise transformation and upgrading: digital transformation, thinking first
猜你喜欢
随机推荐
防范企业数据泄露,就用网络安全产品堡垒机!
Stlink troubleshooting
Cerebral Cortex:从任务态和静息态脑功能连接预测儿童数学技能
C # learning 1: value type and reference type
C learning 2: heap and stack
【云原生】Nacos-TaskManager 任务管理的使用
R语言plotly可视化:plotly可视化多个数据集归一化直方图(historgram)、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图rug
MySQL常用语句和命令汇总
89. (cesium article) cesium aggregation diagram (custom picture)
如何在 WordPress 中嵌入 iFrame
What are the advantages of intelligent chat robots? Senior independent station sellers tell you!
BS-GX-018 基于SSM实现在校学生考试系统
蓝桥杯2015年CA省赛(填坑中)
如何在网站上安装 WordPress
leetcode:42. 接雨水【双指针很优雅】
Go标准库Context包:单个请求多个goroutine 之间与请求域的数据、取消信号、截止时间等相关操作
Timecho of Tianmou technology completed Angel round financing of nearly RMB 100 million and built the original time series database of the industrial Internet of things around Apache iotdb
golang操作NSQ分布式消息队列
Mysql database Basics: introduction to data types
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapuosuo and Beiyou proposed category semantic embedding rich in visual information








