python ssh 连接(python pexpect ssh 远程登录服务器的方法)
类别:脚本大全 浏览量:1380
时间:2022-03-29 18:41:32 python ssh 连接
python pexpect ssh 远程登录服务器的方法使用了python中的pexpect模块,在测试代码之前,可输入python进入交互界面,输入help('pexpect'),查询是否本地含有pexpect模块。
如果没有,linux系统输入 easy_install pexpect便可自动安装。
测试代码,连接127.0.0.1
下面是我手动连接127.0.0.1, 发现只有在首次使用ssh连接127.0.0.1时,需要输入yes or no ,而后再次使用ssh ,则不需要再次输入yes
直接输入密码即可。
后续测试代码是二次链接,无需查询是否需要输入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' #测试主机ip或者主机名 user = 'root' #测试账号 password = 'root' #测试密码 child = connect(user, host, password) send_command(child, 'w' ) if __name__ = = '__main__' : main() |
可以用pxssh模块更简单来完成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 远程登录服务器的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/AcSuccess/article/details/72867959
您可能感兴趣
- python怎么爬取excel数据(python爬取内容存入Excel实例)
- python详细讲解类方法的使用(浅谈python标准库--functools.partial)
- python发送微信消息脚本(python实现微信每日一句自动发送给喜欢的人)
- python生成效果图(python如何制作缩略图)
- centos8配置python开发环境(CentOS6.9 Python环境配置python2.7、pip、virtualenv)
- pythonlist类型教程(Python List cmp知识点总结)
- python递归深度遍历多叉树(Python实现二叉树的常见遍历操作总结7种方法)
- python网页自动化selenium(python利用selenium进行浏览器爬虫)
- pyqt一键部署python环境(python使用PyQt5的简单方法)
- python常用的属性和方法(Python进阶之@property动态属性的实现)
- python群聊提示(python-itchat 统计微信群、好友数量,及原始消息数据的实例)
- python turtle简易绘图(详解Python使用Plotly绘图工具,绘制甘特图)
- python人脸识别库(20行python代码实现人脸识别)
- python读写文件实验心得(Python文件读写常见用法总结)
- python什么是深拷贝什么是浅拷贝(Python深拷贝与浅拷贝用法实例分析)
- python中读取文件怎么操作(Python实现的读取文件内容并写入其他文件操作示例)
- 小敏家 剧情离谱一锅乱炖,但他们俩绝对是这部剧的一大 亮点(剧情离谱一锅乱炖)
- 《倚天屠龙记》再遭翻拍,关晓彤主演赵敏,蒋劲夫演张无忌,你怎么看(倚天屠龙记再遭翻拍)
- 吴启华与曾舜晞两代张无忌同框,戏里经典的他却没活出原著的潇洒(吴启华与曾舜晞两代张无忌同框)
- 经常发这三种 朋友圈 的人,要迅速屏蔽(经常发这三种朋友圈)
- 有种尴尬叫朋友圈忘屏蔽,大学生上演社死现场,父母亲自下场吐槽(有种尴尬叫朋友圈忘屏蔽)
- 朋友圈屏蔽你的人,可以直接看淡了(朋友圈屏蔽你的人)
热门推荐
- python写的工具(python调用虹软2.0第三版的具体使用)
- 搭建web服务器的步骤(windows下三种web服务器搭建方式)
- tomcat原理详解(解析Tomcat架构原理到架构设计)
- mysql mvcc 隔离级别(详解MySQL事务的隔离级别与MVCC)
- apachephp环境搭建(apache集成php7.3.5的详细步骤)
- iis安全包括哪些方面(使用华盾IIS备份还原工具备份还原IIS站点图解)
- Web开发中的几个建议
- oracle如何用脚本文件创建表空间(MySQL版oracle下scott用户建表语句实例)
- css弹出框特效(利用CSS实现酷炫的下拉框特效)
- C#中ToString()常见的格式
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9