1

I'm no programmer, but I try modify the following script.

http://www.networking-forum.com/wiki/Python_SSH_Script

I would like to make the script a bit more efficient. At the moment the for loop makes the script do a new login for each command.

I would like the script do one login for each device and run all commands with one output for each device.

Here is the for loop:

# This function loops through devices. No real need for a function here, just doing it.
  def connect_to(x):
      for device in x:
          # This strips \n from end of each device (line) in the devices list
          device = device.rstrip()
          # This opens an SSH session and loops for every command in the file
          for command in commands:
              # This strips \n from end of each command (line) in the commands list
              command = command.rstrip()
              ssh = paramiko.SSHClient()
              ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
              ssh.connect(device, username=username, password=password)
              stdin, stdout, stderr = ssh.exec_command(command)
              output = open(device + ".out", "a")
              output.write("\n\nCommand Issued: "+command+"\n")
              output.writelines(stdout)
              output.write("\n")
              print "Your file has been updated, it is ", device+".out"
              ssh.close()

  connect_to(devices)
  f1.close()
  f2.close()
  # END 

1 Answers1

0

After looking at the correct indentation found on your source check out the modifications below. This was inspired from this SO answer.

NOTE I do not have a target to ssh into and test these modifications.

def connect_to(x):
    for device in x:
        # Connect to the target 
        device = device.rstrip()
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(device, username=username, password=password)

        # Open up a stream for the conncction
        channel = ssh.invoke_shell()
        ssh_stdin = channel.makefile('wb')
        ssh_stdout = channel.makefile('rb')
        output = open(device + ".out", "a")

        # Send all of the commands to the open session
        for command in commands:
            # This strips \n from end of each command (line) in the commands list
            command = command.rstrip()

            # send the command
            ssh_stdin.write(command)

            # Update the local log file
            output.write("\n\nCommand Issued: "+command+"\n")
            output.writelines(ssh_stdout.read())
            output.write("\n")
            print "Your file has been updated, it is ", device+".out"

        # Close the connection after all of the commands have been issued
        output.close()
        ssh_stdin.close()
        ssh_stdout.close()
        ssh.close()
Community
  • 1
  • 1
Adam Lewis
  • 7,017
  • 7
  • 44
  • 62
  • Sorry it didn't work. No output file and no ssh connection from the script. – user2737247 Sep 01 '13 at 17:43
  • @user2737247 Where there any exceptions thrown? – Adam Lewis Sep 01 '13 at 18:38
  • Not quite sure what you mean. The script runs just fine, but no ssh connection is made and no output file is written. – user2737247 Sep 01 '13 at 18:48
  • @user2737247 Not sure whats going on then. When I get back to a linux box I'll take a look at again. Sorry I couldn't be of more use. This might be a dumb question but did you do a copy and replace on the end of the file? I left off the call to `connect_to_devices`. – Adam Lewis Sep 01 '13 at 18:59
  • Sorry my fault. Didn't include the "connect_do_devices". But it stil doesn't work. Sorry my fault. Didn't include the "connect_do_devices". But it stil doesn't work. Traceback (most recent call last): File "./ssh_commands_2.py", line 100, in connect_to(devices) File "./ssh_commands_2.py", line 87, in connect_to ssh_stdin.write(command) File "/usr/local/lib/python2.7/dist-packages/paramiko/file.py", line 314, in write self._write_all(data) – user2737247 Sep 02 '13 at 03:43
  • File "/usr/local/lib/python2.7/dist-packages/paramiko/file.py", line 439, in _write_all count = self._write(data) File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 1256, in _write self.channel.sendall(data) File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 796, in sendall raise socket.error('Socket is closed') socket.error: Socket is closed – user2737247 Sep 02 '13 at 03:45