python---pexpect的ssh模拟ssh登陆,针对个别主机命令输入
2017-12-25 16:16
489 查看
python—pexpect的ssh模拟ssh登陆,针对个别主机命令输入
2、运行情况(此脚本缺陷是运行只能显示最后一行的信息)
参考:
http://blog.csdn.net/SKI_12/article/details/72972238?locationNum=2&fps=1
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---pexpect的pxssh进行模拟ssh登陆返回相关命令信息
- linux 使用java编写ssh登陆主机 输入密码登陆并执行命令(jsch)
- python---pexpect的ssh模拟ssh登陆
- Python的Pexpect详解[类似shell与ftp/ssh等命令交互]
- ssh 登陆linux主机不需要输入密码方法
- Python模拟登陆3: 进入选课系统(手工输入验证码阶段)
- shell中使用ssh登陆到另一台主机后执行相应的命令
- web测试常用python代码——ssh远程登陆以及命令执行
- ssh登陆命令行中输入密码并且远程执行命令
- 使用ssh-keygen命令生成密钥对无密码远程登陆linux主机
- shell中使用ssh登陆到另一台主机后执行相应的命令
- Python SSH登陆--pexpect,pxssh
- web测试常用python代码——ssh远程登陆以及命令执行
- 项目总结(三)----------Python实现SSH远程登陆,并执行命令!
- Python实现SSH远程登陆,并执行命令!
- python模拟浏览器登陆淘宝(设置代理、输入验证码)
- 使用pexpect提供的SSH模块登陆linux,并执行命令
- python,pexpect登录ssh并执行命令
- python---使用pxssh模块模拟ssh登录linux主机