1、paramiko自動登入網路裝置抓取配置資訊
ssh =paramiko.sshclient()ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
ssh.connect(hostname='
192.168.1.2
', port=22, username='
cisco
', password='
cisco
')client =ssh.invoke_shell()
def run_cmd(cmd, endswith): #
形參:cmd命令,結束符
buff = ''
client.send(cmd)
while
notbuff.endswith(endswith):
resp = str(client.recv(1024), '
utf-8')
buff +=resp
return
buff
res = ''
res += run_cmd('
enable\n
', '
password: ')
res += run_cmd('
cisco\n
', '#'
)res += run_cmd('
terminal length 0\n
', '#'
)res += run_cmd('
show run\n
', '#'
(res)
ssh.close()
defsshconnect(command,hostname):
""":param command: 格式必需為list
:param hostname: 單個主機ip位址
:return: 使用者不存在或連線超時返回錯誤
"""ssh =paramiko.sshclient()
ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
try:
ssh.connect(hostname=hostname, port=22, username=username, password=password)
except
(novalidconnectionserror,timeouterror,authenticationexception) as e:
return
else
: client =ssh.invoke_shell()
for i in
range(0, len(command)):
buff = ''
client.send(command[i] + '\n'
)
while
not buff.endswith('#'
): resp = str(client.recv(1024), '
utf-8')
buff +=resp
ssh.close()
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import
paramiko
class
comupload(object):
def__init__(self, hostname, username='
root
', port=22, password='
123456'):
#self.private_key = paramiko.rsakey.from_private_key_file('/root/.ssh/id_rsa')
self.hostname =hostname
self.username =username
self.port =port
self.password =password
self.transport =paramiko.transport((self.hostname, self.port))
#self.transport.connect(username=self.username, pkey=self.private_key)
self.transport.connect(username=self.username, password=self.password)
self.sftp =paramiko.sftpclient.from_transport(self.transport)
self.client =paramiko.sshclient()
self.client.set_missing_host_key_policy(paramiko.autoaddpolicy())
#允許連線不存在在know_hosts檔案裡的主機
#self.client.connect(hostname=self.hostname, port=self.port, username=self.username, pkey=self.private_key)
self.client.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password)
defupload(self, local_path, remote_path):
#將檔案上傳至伺服器
self.sftp.put(local_path, remote_path)
defdownload(self, remotepath, localpath):
#self.sftp.get(remotepath, localpath)
defcomand(self, com):
#執行命令,返回結果
stdin, stdout, stderr =self.client.exec_command(com)
result =stdout.read().decode()
reserr =stderr.read().decode()
return
result, reserr
defexec_com(self, com):
#執行命令,返回命令結果和狀態碼
self.channel =self.client.get_transport().open_session()
self.channel.exec_command(com)
stdout =self.channel.makefile().read()
stderr =self.channel.makefile_stderr().read()
exit_code =self.channel.recv_exit_status()
self.channel.close()
return
stdout, stderr, exit_code
defsshclose(self):
self.sftp.close()
self.client.close()
if__name__ == '
__main__':
ssh_sftp = comupload('
172.18.188.5')
r, re = ssh_sftp.comand('
echo hello')
res, reserr, exit_code = ssh_sftp.exec_com('
echo helloworld')
ssh_sftp.sshclose()
2、scapy模組(網路掃瞄模組)
pip install scapy #安裝
from scapy.all import * #匯入
ls():顯示支援所有協議
lsc():顯示支援所有命令(就是傳送、接受資料報函式)
help(協議|命令):顯示協議、命令詳細幫助
注意:協議、命令不加單引號或者雙引號,help(snmp)、help(tcpdump)
[root@localhost ~]# git clone
[root@localhost ~]# cd scapy/
[root@localhost ~]# ./run_scapy
python 常用模組
1.告訴直譯器 找模組 import sysunix要絕度路徑 只有第一次匯入執行。name main 2.當做包,必須包含乙個命名為 init py的檔案 模組 3.dir看模組裡有什麼 下劃線開始,不是給模組外部用的。過濾 import copy n for n in dir copy if n...
python常用模組
logging 日誌是我們排查問題的關鍵利器,寫好日誌記錄,當我們發生問題時,可以快速定位 範圍進行修改 logging將日誌列印到螢幕,日誌級別大小關係為 critical error warning info debug notset,當然也可以自己定義日誌級別 預設logging預設的日誌級別...
python常用模組
collections提供了幾個便於使用的資料型別。1 namedtuple 這個資料型別生成可以使用呼叫屬性的方法來訪問元素內容的元祖 import collections cc collections.namedtuple sha x y get cc 1,2 print get.x,get.y...