詳解python連接telnet和ssh的兩種方式
更新時間:2021年10月29日 16:23:31 作者:dudu_mer
本文主要介紹了python連接telnet和ssh的兩種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
Telnet 連接方式
#!/usr/bin/env python
# coding=utf-8
import time
import telnetlib
import logging
__author__ = 'Evan'
save_log_path = 'result.txt'
file_mode = 'a+'
format_info = '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# 添加記錄 記錄器功能
fh = logging.FileHandler(save_log_path, mode=file_mode)
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter(format_info))
logger.addHandler(fh)
# 增加顯示 記錄器功能
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:
#獲取登錄提示‘login:' 后輸入密碼。
handle.read_until('login:', timeout=5)
#發(fā)送命令 登錄,用戶名:admin 密碼:admin
handle.write('admin\n') #用戶名
#如果有輸入密碼的提示符可以打開這一條,并修正確的密碼提示符
#handle.read_until('輸入密碼提示符', timeout=5)
time.sleep(1)
handle.write('admin\n') #密碼
time.sleep(1)
handle.write('en\n') #執(zhí)行指令
time.sleep(1)
handle.write('sys\n') #執(zhí)行指令
time.sleep(1)
handle.write('display running-config\n') #執(zhí)行指令
time.sleep(1)
handle.write('show stack\n') #執(zhí)行指令
time.sleep(1)
#讀取所有信息
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連接方式
#!/usr/bin/env python
# coding=utf-8
import paramiko,sys,time
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#連接SSH服務(wù)器
client.connect("192.168.10.1",22,"admin","admin")
#執(zhí)行命令的方式一 連接linux發(fā)送固定指令
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())
#執(zhí)行命令的方式二 獲取命令行參數(shù),并且刪除參數(shù)1.保留需要執(zhí)行的命令
buf = sys.argv
del buf[0]
str1 = ' '.join(buf)
print(str1)
#執(zhí)行命令行參數(shù)給出的命令
stdin,stdout,stderr = client.exec_command(str1)
#time.sleep(1)
print(stdout.read())
到此這篇關(guān)于詳解python連接telnet和ssh的兩種方式的文章就介紹到這了,更多相關(guān)python連接telnet和ssh內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中如何用time方法生成當(dāng)前時間年月日時分秒
這篇文章主要給大家介紹了關(guān)于python中如何用time方法生成當(dāng)前時間年月日時分秒的相關(guān)資料,在Python中與時間處理有關(guān)的模塊就包括:time,datetime以及calendar,Time模塊用以取得系統(tǒng)時間相關(guān)的信息和時間的格式化等操作,需要的朋友可以參考下2023-08-08
Django實現(xiàn)web端tailf日志文件功能及實例詳解
這篇文章主要介紹了Django實現(xiàn)web端tailf日志文件功能,本文通過實例給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
python實現(xiàn)差分隱私Laplace機(jī)制詳解
今天小編就為大家分享一篇python實現(xiàn)差分隱私Laplace機(jī)制詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
python通過PyQt5實現(xiàn)登錄界面的示例代碼
本文主要介紹了python通過PyQt5實現(xiàn)登錄界面的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

