Advertisement
jtarjanyi

ossi1

Jan 27th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. #!/usr/bin/python
  2. # This Python file uses the following encoding: utf-8
  3.  
  4. from pexpect import pxssh
  5. import getpass
  6. import argparse
  7. import os
  8. import csv
  9.  
  10. # COMMAND_PROMPT = '[$#] '
  11. # TERMINAL_PROMPT = '^Enter.+'
  12. # TERMINAL_TYPE = 'vt100'
  13. # SSH_NEWKEY = 'are you sure you want to continue connecting.*'
  14.  
  15. """Handle imput paramaters"""
  16.  
  17. parser = argparse.ArgumentParser(description='''My Description. \
  18.        And what a lovely description it is. ''', epilog="""All's well that ends well.""")
  19. parser.add_argument("host", help="Host name or address, where want to connect")
  20. parser.add_argument("username", help="Username for host")
  21. parser.add_argument("-p", "--password", help="Password for host")
  22. parser.add_argument("-i", "--inputfile", help="Input file name (CSV)")
  23. parser.add_argument("-o", "--outputfile", help="Output file name")
  24. parser.add_argument("-c", "--command", help="CM command as a string; \
  25.                    eg 'display station xxxx'")
  26. parser.add_argument("-f", "--fieldID", help="FieldID /what you want t change/")
  27. parser.add_argument("-d", "--data", help="data for change command")
  28. args = parser.parse_args()
  29.  
  30. # Open and read input csv file if defined
  31. if args.inputfile is not None:
  32.     try:
  33.         info = csv.reader(open(args.inputfile))
  34. #        for row in info:
  35. #            print row[0]
  36.     except:
  37.         print ("Failed to open: ", args.inputfile)
  38.  
  39. if args.outputfile is not None:
  40.     try:
  41.         with open(args.outputfile, "w+") as out_f:
  42.             print "Output file successfully opened"
  43.         #    out_f.write("Ezt irjuk bele a fileba. \nEz pedig a következő sor \n")
  44.     except:
  45.         print ("File open waas not succesful - need to investigate why :P")
  46.  
  47. try:
  48.     s = pxssh.pxssh()
  49.     # hostname = raw_input('hostname: ')
  50.     # username = raw_input('username: ')
  51.     if args.password is not None:
  52.         password = args.password
  53.     else:
  54.         password = getpass.getpass('password: ')
  55.  
  56.     # print args.host, args.username, password
  57.     s.login(args.host, args.username, password, terminal_type='vt100', original_prompt='[#$>t\]]')
  58.     s.logfile = open("log.txt", "wb")
  59.     print " - Connection established - "
  60.     s.sendline('sat')   # run a command
  61.     s.expect('Terminal Type.*')             # match the prompt
  62.     s.sendline('ossit')
  63.     s.expect('t')             # match the prompt
  64.     print ' - ossi is logged in and ready - '
  65.     s.sendline('cli usa ext 5000')
  66.     s.sendline('t')
  67.     # print s.readline()
  68.     # s.timeout = 5
  69.     s.expect('t\r\n\r')
  70.     # s.sendline('y')
  71.     # print(s.before)
  72.     # s.expect('more')
  73.     # s.sendline('y')
  74.     # print(s.before)
  75.  
  76.     print (s.before)
  77.     print (' - Logging out from ossi - ')
  78.     s.sendline('clogoff')
  79.     s.sendline('t')
  80.     s.expect('Proceed With Logoff.*')
  81.     s.sendline('y')
  82.     s.prompt()
  83.     # print(s.before)
  84.     print' - ossi logged out - '
  85.     s.logout()
  86.     print 'logged out'
  87. except pxssh.ExceptionPxssh as e:
  88.     print("pxssh failed on login.")
  89.     print(e)
  90.  
  91. # def ossirun(cmd, atbs):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement