Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Threading with python in sending multiple commands?
#1
Hi, Anyone here tried threading method in sending command to cisco device? Currently having issues moving from main to beg_rm function using the below code for threading.

My functions:
def ping_ip(ip)
def send_cmd(?)
def beg_rm(ip,uname,ppass,enpass,lcmd):
def main()

<CUT>
#def send_cmd()
#run a single command to devices?

def beg_rm(ip,uname,ppass,enpass,lcmd):
    print "Begin remote connection",ip
    child = pexpect.spawn('ssh %s@%s' %(uname, ip))
    try:
        i = child.expect(['Password: ', 'Username: ','(yes\/no)? ','#',pexpect.EOF,pexpect.TIMEOUT],timeout=5)
	if i == 0:
            child.sendline(ppass)
            child.expect('>')
            child.sendline('enable')
            child.expect('Password: ')
            child.sendline(enpass)
	    elif i == 1:
            child.sendline(uname)
            child.expect('Password: ')
            child.sendline(ppass)
            child.expect('>')
            child.sendline(enpass)
        elif i == 2:
	        child.sendline('yes')
            child.expect('Password: ')
            child.sendline(ppass)
            child.expect('>')
            child.sendline('enable')
            child.expect('Password: ')
            child.sendline(enpass)
            return 'OK'
	elif i == 3:
	    pass

#Push Commands
	for w in lcmd: 
	    #coms = w.strip()
                print "\n-- Processing command ",w
                child.expect ('#')
                child.sendline(w)
                child.expect ('#')
                #print "Executed command", cmd
                time.sleep(1)
                show = str(child.before)
                print(show)
    except:
        print "Error accessing the device"
	return "Failed"

	    
def main():
    print('-'*50)
    while True:
        print('------------------------- ue Network Tools -------------------------------')
        print('--- *********************************************************************** ---')
        print('-'*80)
        print'[1] Troubleshoot'
        print'[2] Custom Tshoot'
        print'[3] Wireless LAN'
        print'[4] Confinder'
        print'[q] exit\n'

        #Select Function
        input_select = raw_input('Please select a function: ')
        input_select = str(input_select)

        if input_select == 'q' or input_select == 'Q':
            sys.exit()
        elif input_select == '1':
            #Read the txt file
	    devtxt = open('devices.txt')
            devlist  = devtxt.read().splitlines()
            print devlist
	    cmdtxt = open('command.txt')
            cmdlist  = cmdtxt.read().splitlines()
	    print cmdlist

            tuname = raw_input("TACACS Username: ")
            tpass=getpass.getpass("TACACS Password: ")
            epass=getpass.getpass("Enable Password: ")

	    #LIST
	    gips = []
	    threadslist = []

            #Verify Reachability
            for ips in devlist:
                print "Processing the list to function", ips
                response = ping_ip(ips)
                result = ('%s \n' % (response))
		print result
                if re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})',str(response)):
		    forgips = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})',str(response))
                    strgips = ''.join(forgips)
		    #print "FORGIPS 2",strgips
 		    gips.append(strgips)
		    pass
		else:
		    pass
	    
	    print "\nList of reachable devices to be sent for threading:\n", gips

#HAVING ISSUE ON THIS PART ITHINK
	    #try:
	    for x in gips:
  		x = x.strip()
		print "\nInitiate connection to ",x
		t = threading.Thread(target = beg_rm, args = (x,tuname,tpass,epass,cmdlist))
		threadslist.append(t)
		t.start
	    for t in threadslist:
		t.join()
#        else:
#	    print "Exiting"

if __name__ == '__main__':
    main()

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to run local python script to remote machine without sending krishna1989 1 684 Feb-21-2019, 05:18 PM
Last Post: marienbad
  Python Regex multiple search into separate variable? searching1 11 1,101 Jan-04-2019, 07:37 PM
Last Post: nilamo
  Sending DNS responses with python - research dnsman2018 0 714 Aug-05-2018, 12:01 PM
Last Post: dnsman2018
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 794 Jul-30-2018, 07:52 AM
Last Post: Lyperion
  Is the paramiko's API threading safe? bsxfun 0 1,509 May-18-2018, 02:56 PM
Last Post: bsxfun
  Threading - RPi server UDP KoSik 3 900 Feb-26-2018, 08:54 AM
Last Post: CaptainBlueballs
  Sending response delay michalt38 1 827 Feb-13-2018, 03:19 PM
Last Post: sparkz_alot
  Python script for show commands-CISCO Devices babbarsher 1 4,130 Dec-13-2017, 11:44 PM
Last Post: Larz60+
  Sending data over UDP alidaf 3 1,575 Oct-19-2017, 03:39 PM
Last Post: Larz60+
  Trouble with sending raw sockets IronReign 2 1,766 Jun-01-2017, 08:26 AM
Last Post: camp0

Forum Jump:


Users browsing this thread: 1 Guest(s)