您的位置:首页 > 编程语言 > Python开发

python---pexpect的ssh模拟ssh登陆,针对个别主机命令输入

2017-12-25 16:16 489 查看
python—pexpect的ssh模拟ssh登陆,针对个别主机命令输入

root@kali:~/python/anquangongji# clear
root@kali:~/python/anquangongji# pwd
/root/python/anquangongji
root@kali:~/python/anquangongji# ls
checkfile.py      dictionary.txt   passwordunix.txt        pexpectshhusecommand.py  scanmultports.py  vulnftpbanner.txt
crarkpassword.py  openfiletest.py  pexpectshhnocommand.py  scanerftpbanner.py       vulnbanners.txt
root@kali:~/python/anquangongji# cat pexpectshhusecommand.py
#!/usr/bin/python
#coding=utf-8
import pexpect
from optparse import OptionParser

#SSH连接成功时的命令行交互窗口中前面的提示字符的集合
PROMPT = ['# ','>>> ','> ','\$ ']

def send_command(child,cmd):
#发送一条命令
child.sendline(cmd)

#期望有命令行提示字符出现
child.expect(PROMPT)

#将之前的内容都输出
print child.before.split("\n")[1]

def connect(user,host,password):
#表示主机已使用一个新的公钥的消息
ssh_newkey = 'Are you sure you want to continue connecting'
connStr = 'ssh ' + user + '@' + host

#为ssh命令生成一个spawn类的对象
child = pexpect.spawn(connStr)

#期望有ssh_newkey字符、提示输入密码的字符出现,否则超时
ret = child.expect([pexpect.TIMEOUT,ssh_newkey,'[P|p]assword: '])

#匹配到超时TIMEOUT
if ret == 0:
print '[-] Error Connecting'
return

#匹配到ssh_newkey
if ret == 1:
#发送yes回应ssh_newkey并期望提示输入密码的字符出现
child.sendline('yes')
ret = child.expect([pexpect.TIMEOUT,ssh_newkey,'[P|p]assword: '])

#匹配到超时TIMEOUT
if ret == 0:
print '[-] Error Connecting'
return

#发送密码
child.sendline(password)
child.expect(PROMPT)
return child

def main():
parser = OptionParser("[*] Usage : python pexpectshhusecommand.py -H <target host> -u <username> -p <password>")
parser.add_option("-H" ,dest="host",type="string",help = "specify targrt host")
parser.add_option("-u",dest="username",type="string",help = "specify targrt username")
parser.add_option("-p",dest="password",type="string",help = "specify targrt password")
(options,args)=parser.parse_args()

if (options.host == None) | (options.username == None) | (options.password == None):
print parser.usage
exit(0)

child=connect(options.username,options.host,options.password)

while True:#一直循环等待命令输入
command = raw_input("< SSH >")
send_command(child,command)

if __name__ == '__main__':
main()

root@kali:~/python/anquangongji#


2、运行情况(此脚本缺陷是运行只能显示最后一行的信息)

root@kali:~/python/anquangongji# python pexpectshhusecommand.py -H 192.168.100.140 -u root -p 173605852
< SSH >whoami
root
< SSH >pwd
/root
< SSH >ls
a.t        find.py                              new.txt     showNumType.py
< SSH >ls -la
总用量 1532
< SSH >ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:11:91:7b
< SSH >uname -a
Linux kali 3.18.0-kali1-686-pae #1 SMP Debian 3.18.3-1~kali4 (2015-01-22) i686 GNU/Linux
< SSH >^CTraceback (most recent call last):
File "pexpectshhusecommand.py", line 69, in <module>
main()
File "pexpectshhusecommand.py", line 65, in main
command = raw_input("< SSH >")
KeyboardInterrupt
root@kali:~/python/anquangongji#


参考:

http://blog.csdn.net/SKI_12/article/details/72972238?locationNum=2&fps=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python ssh