将用户名和密码传递给ssh

时间:2017-06-01 16:06:26

标签: python

我在Debian VirtualBox上有以下代码:

#!/usr/bin/env python

import paramiko
import time
import re
import sys

#Open SSHv2 connection to devices
def open_ssh_conn(ip):
    #Change exception message
    try:
        #Defining the credentials file
        user_file = sys.argv[1]

        #Defining the commands file
        cmd_file = sys.argv[2]

        #define the SSH parameters
        selected_user_file = open(user_file, 'r')

        #Starting from beginning of file
        selected_user_file.seek(0)

        #Reading the username from file
        username = selected_user_file.readline()[0].split(',')[0]

        #Starting from the beginning of file
        selected_user_file.seek(0)

        #Reading password from file
        password = selected_user_file.readlines()[0].split(',').rstrip("\n")

        #Logging into device
        session = paramiko.SSHClient()

        #For testing purposes, this allows auto-accepting unknown host keys
        #Do not use in production!! The default would be RejectPolicy
        session.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        #Start and interactive shell session on the router
        connection = session.invoke_shell()

        #Setting terminal length for entire output - disable pagination
        connection.send("\n")
        connection.send("configure terminal\n")
        time.sleep(1)

        #Open user selected file for reading
        selected_cmd_fil = open(cmd_file, 'r')

        #Writing each linein the file to the device
        for each_line in selected_cmd_file.readlines():
            connection.send(each_line + '\n')
            time.sleep(2)

        #Closing the user file
        selected_user_file.close()

        #closint the command file
        selected_cmd_file.close()

        #Expect to receive a maximum amount of 65535 bytes of data and store in a variable
        router_output = connection.recv(65535)

        #Checking command output for IOS syntax errors
        if re.search(r"% Invalid input detected at", router_output):
            print "*There was at lease one IOS syntax error on device %s" % ip
        else:
            print "\nDone for device %s" % ip

        #Test for reading command output
        print router_output + "\n"

        #closing the connection
        session.close
    except paramiko.AuthenticationException:
        print "* Invaliid username or password. \n Please check the usernmame/password file or the device configuration"
        print "* Closing program...\n"

#Calling the SSH function
open_ssh_conn("192.168.2.102")

使用以下user_file:

root @ debian:/ home / debian / workingdir #cat ssh_credentials.txt teopy,蟒

当我尝试运行代码时,收到以下错误消息:

root@debian:/home/debian/workingdir# python SSHTemplate_2.py 
Traceback (most recent call last):
  File "SSHTemplate_2.py", line 81, in <module>
    open_ssh_conn("192.168.2.102")
  File "SSHTemplate_2.py", line 13, in open_ssh_conn
    user_file = sys.argv[1]
IndexError: list index out of range

非常感谢任何帮助。

python SSHTemplate_2.py ssh_credentials telnet_commands.txt是我正在运行的并收到以下错误:

root@debian:/home/debian/workingdir# python SSHTemplate_2.py ssh_credentials.txt telnet_commands.txt  
  

追踪(最近一次呼叫最后一次):

File "SSHTemplate_2.py", line 82, in <module> open_ssh_conn("192.168.2.102")  
File "SSHTemplate_2.py", line 31, in open_ssh_conn  
password = selected_user_file.readlines()[0].split(',').rstrip("\n")  
AttributeError: 'list' object has no attribute 'rstrip' 

1 个答案:

答案 0 :(得分:0)

这是你的错:

#Reading password from file
password = selected_user_file.readlines()[0].split(',').rstrip("\n")

我认为您的user_file数据热线为Username,Password 改为

# Using `with` block will auto close the file on leave the block
with  open(user_file) as fh:
    # Read one Line, 
    # assign to var without the trailing NewLine
    data = fh.readline()[:-1]
    # Change var to Type List of String
    data = data.split(',')
    username = data[0]
    password = data[1]

#Logging into device
session = paramiko.SSHClient()
   ...