python shell交互脚本
#!/usr/bin/python
# coding=utf-8
#from pexpect import pxssh
import paramiko
from optparse import OptionParser
def send_command(ssh, cmd):
std_in, std_out, std_err = ssh.exec_command(cmd, timeout=3 * 1000)
for line in std_out:
print(line.strip("\n"))
for line in std_err:
print(line.strip("\n"))
def connect(host, user, password,port=22):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host,username=user,password=password,port=port)
return ssh
except Exception:
print('[-] Error Connecting',Exception.__str__())
exit(0)
def main():
parser = OptionParser("[*] Usage : ./***.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)
ssh = connect(options.host, options.username, options.password)
try:
while True:
command = input('<SSH>: \n')
send_command(ssh, command)
except:
ssh.close()
if __name__ == '__main__':
main()