python pexpect ssh 遠(yuǎn)程登錄服務(wù)器的方法
使用了python中的pexpect模塊,在測(cè)試代碼之前,可輸入python進(jìn)入交互界面,輸入help('pexpect'),查詢是否本地含有pexpect模塊。
如果沒有,linux系統(tǒng)輸入 easy_install pexpect便可自動(dòng)安裝。
測(cè)試代碼,連接127.0.0.1
下面是我手動(dòng)連接127.0.0.1, 發(fā)現(xiàn)只有在首次使用ssh連接127.0.0.1時(shí),需要輸入yes or no ,而后再次使用ssh ,則不需要再次輸入yes
直接輸入密碼即可。

后續(xù)測(cè)試代碼是二次鏈接,無(wú)需查詢是否需要輸入yes or no
import pexpect
def send_command(child, cmd):
child.sendline(cmd)
child.expect(PROMT)
print child.before
def connect(user, host, password):
ssh_newkey = 'Ary you sure you want to continue connecting'
connStr = 'ssh ' + user + '@' + host
child = pexpect.spawn(connStr)
'''
ret = child.expect([pexpect.TIMEOUT, ssh_newkey])
if ret == 0:
print "[-] Error 1"
return
elif ret == 1:
child.sendline('yes')
'''
res = child.expect([pexpect.TIMEOUT, '[P|p]assword:'])
if res == 0:
print "[-] Error 2"
return
elif res == 1:
child.sendline(password)
child.expect(PROMT)
return child
def main():
host = '127.0.0.1'#測(cè)試主機(jī)ip或者主機(jī)名
user = 'root'#測(cè)試賬號(hào)
password = 'root'#測(cè)試密碼
child = connect(user, host, password)
send_command(child, 'w')
if __name__ == '__main__':
main()
可以用pxssh模塊更簡(jiǎn)單來(lái)完成ssh的連接
from pexpect import pxssh
def send_command(s, cmd):
s.sendline(cmd)
s.prompt()
print s.before
def connect(host, user, password):
try:
s = pxssh.pxssh()
s.login(host, user, password)
return s
except:
print "error"
exit(0)
def main():
s = connect('127.0.0.1', 'root', '15110506010')
send_command(s, 'whoami')
if __name__ == '__main__':
main()
批量連接肉雞。
from pexpect import pxssh
botnet = []
class client:
def __init__(self, user, host, password):
self.user=user
self.host=host
self.password=password
self.child=self.connect()
def connect(self):
try:
s = pxssh.pxssh()
s.login(self.host, self.user, self.password)
return s
except Exception, e:
print "Error *" + str(e)
def send_command(self, cmd):
self.child.sendline(cmd)
self.child.prompt()
return self.child.before
def addclient(user, host, password):
c = client(user, host, password)
botnet.append(c)
def botnetcommand(command):
for c in botnet:
output = c.send_command(command)
print "ip: " + str(c.host)
print output
def main():
addclient('root', '127.0.0.1', 'toor')
addclient('root', '****', '*****')
botnetcommand('pwd')
if __name__=='__main__':
main()
以上這篇python pexpect ssh 遠(yuǎn)程登錄服務(wù)器的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案
- Python3 SSH遠(yuǎn)程連接服務(wù)器的方法示例
- 利用python 更新ssh 遠(yuǎn)程代碼 操作遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)代碼
- 用python寫個(gè)自動(dòng)SSH登錄遠(yuǎn)程服務(wù)器的小工具(實(shí)例)
- python下paramiko模塊實(shí)現(xiàn)ssh連接登錄Linux服務(wù)器
- python操作ssh實(shí)現(xiàn)服務(wù)器日志下載的方法
- 如何使用Python連接?SSH?服務(wù)器并執(zhí)行命令
相關(guān)文章
Python3+OpenCV實(shí)現(xiàn)簡(jiǎn)單交通標(biāo)志識(shí)別流程分析
這篇文章主要介紹了Python3+OpenCV實(shí)現(xiàn)簡(jiǎn)單交通標(biāo)志識(shí)別,主要思路是解析XML文檔,根據(jù)<name>標(biāo)簽進(jìn)行分類,如果是直行、右轉(zhuǎn)、左轉(zhuǎn)、停止就把它從原圖中裁剪下來(lái)并重命名,感謝的朋友跟隨小編一起看看示例代碼2021-12-12
用不到50行的Python代碼構(gòu)建最小的區(qū)塊鏈
這篇文章主要為大家詳細(xì)介紹了用不到50行的Python代碼構(gòu)建最小的區(qū)塊鏈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
AI人工智能 Python實(shí)現(xiàn)人機(jī)對(duì)話
這篇文章主要為大家詳細(xì)介紹了AI人工智能應(yīng)用,本文擬使用Python開發(fā)語(yǔ)言實(shí)現(xiàn)類似于WIndows平臺(tái)的“小娜”,,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Python中使用MELIAE分析程序內(nèi)存占用實(shí)例
這篇文章主要介紹了Python中使用MELIAE分析程序內(nèi)存占用實(shí)例,本文直接給出使用代碼示例,需要的朋友可以參考下2015-02-02
Python實(shí)現(xiàn)多并發(fā)訪問(wèn)網(wǎng)站功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)多并發(fā)訪問(wèn)網(wǎng)站功能,結(jié)合具體實(shí)例形式分析了Python線程結(jié)合URL模塊并發(fā)訪問(wèn)網(wǎng)站的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
感知器基礎(chǔ)原理及python實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了感知器基礎(chǔ)原理及python實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
python面向?qū)ο缶幊淘O(shè)計(jì)原則之單一職責(zé)原則詳解
這篇文章主要為大家詳細(xì)介紹了python面向?qū)ο缶幊淘O(shè)計(jì)原則之單一職責(zé)原則,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
如何使用selenium和requests組合實(shí)現(xiàn)登錄頁(yè)面
這篇文章主要介紹了如何使用selenium和requests組合實(shí)現(xiàn)登錄頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

