Python_用telnet连接其他设备
1 #!/usr/bin/env python27 2 # -*- coding: utf-8 -*- 3 4 5 # 引用telnetlib模块 6 import telnetlib 7 import paramiko 8 import sys 9 import time 10 import os 11 12 reload(sys) 13 sys.setdefaultencoding('utf-8') 14 15 16 def Root_neweasy(HOST): 17 # 登录时候的 账号、 密码 18 user = "root" 19 password = "neweasy" 20 21 # 定义 command 函数:读取关键词,输入对应的代码 22 def command(con, flag, str_=""): 23 data = con.read_until(flag.encode()) 24 print(data.decode(errors='ignore')) 25 con.write(str_.encode() + b"\n") 26 return data 27 28 # 变量tn , 引用telnetlib模块里面的telnet类 29 tn = telnetlib.Telnet(HOST) 30 # 引用 command函数,自动登录账号和输入密码 31 command(tn, "login: ", user) 32 print ("login") 33 if password: 34 command(tn, "Password: ", password) 35 print ("password") 36 37 # 在服务器内执行命令 38 command(tn, "#", "sh /etc/2020-04-22/ceshi.sh") 39 print ("sh /etc/2020-04-22/ceshi.sh") 40 41 command(tn, "#", r" exit") 42 command(tn, "#", "") 43 44 # 执行完之后关闭 变量tn 45 tn.close() 46 47 if __name__ == '__main__': 48 # 服务器ip 49 host_ip = '192.168.1.105' 50 # 下载时使用的端口 51 host_port = 22 52 # 登录时使用的用户名 53 host_username = 'user' 54 # 登录密码 55 host_password = 'password' 56 # 服务器文件存放的路径 57 remote_path = ('/etc/2020-04-22/' + (time.strftime("%Y-%m-%d")) + '/') 58 # 本地存放文件的路径 59 local_path = (r'G:\python\python_background_of_input\\' +(time.strftime("%Y-%m-%d")) + '/') 60 # 执行查询命令 61 Root_neweasy(host_ip)