简体   繁体   English

如何在python脚本中将IP地址从文本文件分配给主机名

[英]How do I assign IP address from text file to hostname in python script

I am able to retreive and assign username and password from text and pass it to ftplib for "ftp.login" 我可以检索文本并从文本分配用户名和密码,并将其传递给ftplib以获取“ ftp.login”

How can I do the same thing for an IP address in a text file that I want to pass to "ftplib.FTP("0.0.0.0")" instead of the (0.0.0.0) want to replace it with "ftplib.FTP(hostname)? 我如何对要传递给“ ftplib.FTP(“ 0.0.0.0”)”的文本文件中的IP地址执行相同的操作,而不是想要将其替换为“ ftplib.FTP”的(0.0.0.0) (主机名)?

here is the script: 这是脚本:

#!/usr/bin/env python

import ftplib
import os
import sys
import paramiko
import datetime
import pickle
import ftplib as ftp
import socket




login = {}
with open('hostname1.txt', 'r') as g:
   for line in g:
       hostname = line.strip()
       login[hostname] = hostname


credentials = {}
with open('Usernames.txt', 'r') as f:
    for line in f:
        username, password = line.strip().split(':')
        credentials[username] = password

try:
        try:
            print "Connecting to 192.168.189.130"
            ftp = ftplib.FTP(hostname)
            ftp.login(username, password)
            ftp.cwd('dir1')
            ftp.retrlines('LIST')
#           ftp.get()

        except ftplib.all_errors as e:
            print(e)
except ftp.login as s:
    print (s)

Thanks "Praneeth" for quick response! 感谢“ Praneeth”的快速回复!

Answered my own question. 回答了我自己的问题。

This works! 这可行!

simple mistake on my part... did not remove the double quotes "" in the () with the ftplib.FTP(hostname) line..... 我这是一个简单的错误。。。没有删除()中带有ftplib.FTP(hostname)行的双引号“”。

Please use it works great! 请使用它效果很好!

#!/usr/bin/env python

import ftplib
import os
import sys
import paramiko
import datetime
import pickle
import ftplib as ftp
import socket




login = {}
with open('hostname1.txt', 'r') as g:
   for line in g:
       hostname = line.strip()
       login[hostname] = hostname


credentials = {}
with open('Usernames.txt', 'r') as f:
    for line in f:
        username, password = line.strip().split(':')
        credentials[username] = password

try:
        try:
            print "Connecting to 0.0.0.0"
            ftp = ftplib.FTP(hostname)
            ftp.login(username, password)
            ftp.cwd('dir1')
            ftp.retrlines('LIST')
#           ftp.get()

        except ftplib.all_errors as e:
            print(e)
except ftp.login as s:
    print (s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中从此文本文件中获取接口名称和 IP 地址 - How to get interface name and IP address from this text file in Python Python如何将用户提供的IP地址写入文件? - Python How do I write a user provided IP address to a file? 需要帮助来创建一个Python脚本,该脚本从txt文件中读取主机名,对主机名执行ping操作,解析IP并将其打印到另一个文本文件 - Need help creating a Python script that reads hostnames from a txt file, pings the hostname, resolves the IP and prints it to another text file Python-IP到主机名脚本 - Python - IP to hostname script Python:如何从FQDN获取IP地址 - Python: How do I get the IP address from a FQDN 如何从Python中的IP地址获取NAPTR记录? - How do I grab a NAPTR record from an IP address in Python? 如何从 python 中的以下元组中获取 IP 地址 - How do i fetch the IP address from the following tuple in python 如何在python中设置“ ip地址”,“ DNS”,“主机名”,“ MAC地址”? - how to setup “ip address”,“DNS”, “hostname”,“MAC address” in python? 如何使用python从主机为vm分配IP地址? - How to assign an ip address to a vm from the host with using python? 从IP地址获取主机名 - Get Hostname from IP Address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM