簡體   English   中英

將用戶名和密碼傳遞給ssh時遇到問題

[英]Having an issue with passing user name and password to ssh

我在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,python

當我嘗試運行代碼時,收到以下錯誤消息:

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' 

這是你的錯:

#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()
   ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM