简体   繁体   中英

Using Paramiko SSH module with csv.reader?

I am currently using the paramiko module to SSH into a list of IP addresses provided as a .csv file then push commands to those IPs. To accomplish this, I am using the csv.reader() to open and read 100 rows of the file which is in the following format.

Serial number, IP Address, MS, RM (MS & RM are pieces of info correlated to IP address)

The Context

This is where Im using csv.reader() to open and read the file to then place information as a dictionary keys .

I then go to nest another dictionary within keys as keys[row[0]] to index IP address and other pieces of info as ep_ip , ms_key and rm_key .

option_dict = open('OptionKeyDict.csv','r')
reader= csv.reader(option_dict)

keys= {}

for row in reader:
    keys[row[0]]= {'IP':row[1],'MS': row[2],'RM': row[3]}

ep_ip = keys[row[0]]['IP']
ms_key= keys[row[0]]['MS']
rm_key= keys[row[0]]['RM']

command= 'xCommand SystemUnit OptionKey Add Key: '

multi_site= command + ms_key
remote_monitor= command + rm_key

Then I go on to introduce the Paramiko logic of the script to take the statements above and SSH into IP address, then execute the multi-site & remote-monitor commands.

for host in keys:
    host= ep_ip
    i = 1

    while True:
        print ("\nTrying to connect to %s (%i/2)" % (host, i))

        try:
           ssh = paramiko.SSHClient()
           ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
           ssh.connect(host, username=username, password=password, look_for_keys=False, timeout= 5)
           print ("\nConnected to %s" % host )
           connection_state = 1
           break

    if connection_state == 1:
        client_shell = ssh.invoke_shell()
        time.sleep(.4)
        client_shell.send(multi_site + "\n")
        time.sleep(.4)
        client_shell.send(remote_monitor + "\n")

So, running this works partially. The script successfully SSH then pushes the commands but it only uses the pieces of information from the last row (IP address, MS & RM) and runs 100 times as there are 100 rows in .csv.

I cant seem to have the script to start at the first row then work its way down each line to the 100th row.

Now that I got the context out of the way... (Sorry, new to StackOverflow)

The Question

What am I missing here that will enable this to happen? I feel its a simple adjustment that Im not seeing.

Any and all feedback would be greatly appreciated. Thank you!

You are not using the host variable in for host in keys: loop at all.

And you can actually use the data as you read them straight away. It does not look like you need to collect the data to keys at all.

for row in reader:
    host = row[1]
    ms_key = row[2]
    rm_key = row[3]

    i = 1

    while True:
        print ("\nTrying to connect to %s (%i/2)" % (host, i))

        try:
           ssh = paramiko.SSHClient()
           ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
           ssh.connect(
               host, username=username, password=password, look_for_keys=False, timeout= 5)
           print ("\nConnected to %s" % host )
           connection_state = 1
           break

    if connection_state == 1:
        client_shell = ssh.invoke_shell()
        time.sleep(.4)
        client_shell.send(multi_site + "\n")
        time.sleep(.4)
        client_shell.send(remote_monitor + "\n")

(btw, the i is never changed from its initial value of 1 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM