Python Forum

Full Version: Threading with python in sending multiple commands?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()