Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # This Python file uses the following encoding: utf-8
- from pexpect import pxssh
- import getpass
- import argparse
- import os
- import csv
- # COMMAND_PROMPT = '[$#] '
- # TERMINAL_PROMPT = '^Enter.+'
- # TERMINAL_TYPE = 'vt100'
- # SSH_NEWKEY = 'are you sure you want to continue connecting.*'
- """Handle imput paramaters"""
- parser = argparse.ArgumentParser(description='''My Description. \
- And what a lovely description it is. ''', epilog="""All's well that ends well.""")
- parser.add_argument("host", help="Host name or address, where want to connect")
- parser.add_argument("username", help="Username for host")
- parser.add_argument("-p", "--password", help="Password for host")
- parser.add_argument("-i", "--inputfile", help="Input file name (CSV)")
- parser.add_argument("-o", "--outputfile", help="Output file name")
- parser.add_argument("-c", "--command", help="CM command as a string; \
- eg 'display station xxxx'")
- parser.add_argument("-f", "--fieldID", help="FieldID /what you want t change/")
- parser.add_argument("-d", "--data", help="data for change command")
- args = parser.parse_args()
- # Open and read input csv file if defined
- if args.inputfile is not None:
- try:
- info = csv.reader(open(args.inputfile))
- # for row in info:
- # print row[0]
- except:
- print ("Failed to open: ", args.inputfile)
- if args.outputfile is not None:
- try:
- with open(args.outputfile, "w+") as out_f:
- print "Output file successfully opened"
- # out_f.write("Ezt irjuk bele a fileba. \nEz pedig a következő sor \n")
- except:
- print ("File open waas not succesful - need to investigate why :P")
- try:
- s = pxssh.pxssh()
- # hostname = raw_input('hostname: ')
- # username = raw_input('username: ')
- if args.password is not None:
- password = args.password
- else:
- password = getpass.getpass('password: ')
- # print args.host, args.username, password
- s.login(args.host, args.username, password, terminal_type='vt100', original_prompt='[#$>t\]]')
- s.logfile = open("log.txt", "wb")
- print " - Connection established - "
- s.sendline('sat') # run a command
- s.expect('Terminal Type.*') # match the prompt
- s.sendline('ossit')
- s.expect('t') # match the prompt
- print ' - ossi is logged in and ready - '
- s.sendline('cli usa ext 5000')
- s.sendline('t')
- # print s.readline()
- # s.timeout = 5
- s.expect('t\r\n\r')
- # s.sendline('y')
- # print(s.before)
- # s.expect('more')
- # s.sendline('y')
- # print(s.before)
- print (s.before)
- print (' - Logging out from ossi - ')
- s.sendline('clogoff')
- s.sendline('t')
- s.expect('Proceed With Logoff.*')
- s.sendline('y')
- s.prompt()
- # print(s.before)
- print' - ossi logged out - '
- s.logout()
- print 'logged out'
- except pxssh.ExceptionPxssh as e:
- print("pxssh failed on login.")
- print(e)
- # def ossirun(cmd, atbs):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement