In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "what are the two ways for python to connect telnet and ssh". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the two ways for python to connect telnet and ssh?"
Telnet connection method #! / usr/bin/env python# coding=utf-8 import timeimport telnetlibimport logging _ author__ = 'Evan' save_log_path =' result.txt'file_mode ='% (asctime) s -% (filename) s [line:% (lineno) d] -% (levelname) s:% (message) s' logger = logging.getLogger (_ _ name__) logger.setLevel (logging.DEBUG) # add record Recorder function fh = logging.FileHandler (save_log_path Mode=file_mode) fh.setLevel (logging.DEBUG) fh.setFormatter (logging.Formatter (format_info)) logger.addHandler (fh) # add display recorder function ch = logging.StreamHandler () ch.setLevel (logging.DEBUG) ch.setFormatter (logging.Formatter (format_info)) logger.addHandler (ch) def telnet_handle (host='', port=''): handle = telnetlib.Telnet (host, port) Timeout=10) handle.set_debuglevel (2) # Display connect info (send command & received info) logger.debug ('Connect host: {} port: {} successful'.format (host, port)) try: # get the login prompt' login:' and enter the password. Handle.read_until ('login:', timeout=5) # sends a command to log in, user name: admin password: admin handle.write (' admin\ n') # user name # if you have a prompt to enter a password, you can open this one. And fix the correct password prompt # handle.read_until ('enter password prompt' Timeout=5) time.sleep (1) handle.write ('admin\ n') # password time.sleep (1) handle.write ('en\ n') # execute instruction time.sleep (1) handle.write ('sys\ n') # execute instruction time.sleep (1) handle.write ('display running-config\ n') # execute Line instruction time.sleep (1) handle.write ('show stack\ n') # execute instruction time.sleep (1) # read all information result = handle.read_very_eager () logger.info ('Received info: {}' .format (result)) finally: handle.close () if _ _ name__ = ='_ _ main__ ': telnet_handle (host='192.168.10.1' Port='23') ssh connection method #! / usr/bin/env python# coding=utf-8 import paramiko,sys,time client = paramiko.SSHClient () client.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # connection to SSH server client.connect ("192.168.10.1", 22, "admin", "admin") # execution mode A connection to linux sends a fixed instruction stdin,stdout Stderr = client.exec_command ("whoami") time.sleep (2) print (stdout.read ()) stdin,stdout,stderr = client.exec_command ("cat / root/lzhi/c_call_python.txt") print (stdout.read ()) stdin,stdout,stderr = client.exec_command ("ls") print (stdout.read ()) stdin,stdout,stderr = client.exec_command ("ls-la") print (stdout.read ()) # way to get command line arguments And delete parameter 1. Keep the command buf = sys.argvdel buf [0] str1 =''.join (buf) print (str1) # execute the command given by the command line parameter stdin,stdout,stderr = client.exec_command (str1) # time.sleep (1) print (stdout.read ()) so far, I believe you have a deeper understanding of "what are the two ways python connects telnet and ssh", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.