[英]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.