用Pxssh實現與SSH的簡單互動
阿新 • • 發佈:2018-12-23
#!/usr/bin/python #coding=utf-8 from optparse import OptionParser from pexpect import pxssh def send_command(child,cmd): child.sendline(cmd) #匹配prompt(提示符) child.prompt() #將prompt前所有內容打印出 print child.before def connect(host,user,password): try: child = pxssh.pxssh() #利用pxssh類的login()方法進行ssh登入 child.login(host,user,password) return child except: print '[-] Error Connecting' exit(0) def main(): parser = OptionParser("[*] Usage : ./pxSsh.py -H <target host> -u <username> -p <password>") parser.add_option('-H',dest='host',type='string',help='specify target host') parser.add_option('-u',dest='username',type='string',help='target username') parser.add_option('-p',dest='password',type='string',help='target password') (options,args) = parser.parse_args() if (options.host == None) | (options.username == None) | (options.password == None): print parser.usage exit(0) child=connect(options.host,options.username,options.password) while True: #raw_input將所有輸入作為字串看待,不管使用者輸入什麼型別的都會轉變成字串 command = raw_input('<SSH> ') send_command(child,command) if __name__ == '__main__': main()