Python Forum
Threading with python in sending multiple commands?
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python + PHP. Sending data from "Moodle" (php) to python CGI-script and get it back stanislav 0 1,508 May-07-2022, 10:32 AM
Last Post: stanislav
  How to run local python script to remote machine without sending krishna1989 1 8,280 Feb-21-2019, 05:18 PM
Last Post: marienbad
  Sending DNS responses with python - research dnsman2018 0 4,907 Aug-05-2018, 12:01 PM
Last Post: dnsman2018
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 10,406 Jul-30-2018, 07:52 AM
Last Post: Lyperion
  Python script for show commands-CISCO Devices babbarsher 1 12,935 Dec-13-2017, 11:44 PM
Last Post: Larz60+
  sending email with python shahpy 4 14,051 Oct-03-2016, 10:00 PM
Last Post: shahpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020