16 Star 44 Fork 45

现任明教教主-乾颐堂 / qytang_Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
2016.03.23 ssh连接并执行命令 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-03-23 10:12 . new file
#!/usr/bin/python3.4
# -*- coding=utf-8 -*-
#本脚由亁颐堂现任明教教主编写,用于乾颐盾Python课程!
#教主QQ:605658506
#亁颐堂官网www.qytang.com
#乾颐盾是由亁颐堂现任明教教主开发的综合性安全课程
#包括传统网络安全(防火墙,IPS...)与Python语言和黑客渗透课程!
import paramiko
import re
def ssh_connect(ip, passwd, user = 'root', cmd = 'cat /etc/shadow | grep root', port = 22):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,user,passwd,timeout=5)
stdin,stdout,stderr = ssh.exec_command(cmd)
x = stdout.read().decode()
return (1,x)
except Exception as e:
if re.search('Authentication failed.', str(e)):
return (0,'密码或者用户名错误')
else:
return (0,e)
if __name__ == '__main__':
print(ssh_connect('202.100.1.139', 'Cisc01222', 'root'))
"""
[root@Fedora hacker]# ./ssh.py
(1, 'root:$6$LJWOujALo/so9DC2$pCnOrVW8Oo0HCgKqcbL6SsTSb28KGqDSNGmCePQvXliUXx18b.PD4aU50X.DAlNMKx9Jq.RUBsxWLtW1YFM0D0::0:99999:7:::\n')
[root@Fedora hacker]# ./ssh.py
(0, '密码或者用户名错误')
"""
Python
1
https://gitee.com/qytang/qytang_Python.git
git@gitee.com:qytang/qytang_Python.git
qytang
qytang_Python
qytang_Python
master

搜索帮助