`
isiqi
  • 浏览: 16067583 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

linux ftp .netrc 自动登录脚本 auto-login

阅读更多

[root@localhost ~]# cat .netrc
machine hostname
login root
password rootpassword
macdef init
cd /root/
bin
put a.txt
quit

机器 hostname

username 用户名

password 密码

宏脚本 macdef

init自动执行

引用: http://www.mavetju.org/unix/netrc.php

How to use ftp in combination with .netrc

After answering a number of questions regarding automatic FTP on Unix-systems and the involvement of .netrc in it, I created this little guide.

Permissions and location of the .netrc

The .netrc should be in your home-directory and the permissions on the file should be unreadable for everybody except the owner:

[~] edwin@k7>ls -al .netrc

-rw-------  1 edwin  wheel  246 Aug 27 16:14 .netrc

You can set to these settings with chmod 600 .netrc .

Layout of the .netrc

Machine definitions

The first part of the .netrc is filled up with host-definitions:

machine ftp.freebsd.org
	login anonymous
	password edwin@mavetju.org

machine myownmachine
	login myusername
	password mypassword

What it is saying now is nothing more than "If you connect to ftp.freebsd.org , login as anonymous and use edwin@mavetju.org as password." and "If you connect to myownmachine , login as myusername and use mypassword as password.".

Macro definitions

This part of the .netrc consists of macros which can be used to perform automated tasks:

macdef uploadtest
	cd /pub/tests
	bin
	put filename.tar.gz
	quit

macdef dailyupload
	cd /pub/tests
	bin
	put daily-$1.tar.gz
	quit

Keep in mind that there should be an empty line after the last macdef statement. If you don't do this, ftp will complain about it.

The first one is saying "Go to the /pub/tests directory, switch to binary mode, put a file there and quit it". The second one is saying the same, except that the name of the file is based on a parameter on the macro-call (see Usage of the .netrc for more about this).

Usage of the .netrc

Macros can be called from both inside ftp:

[~] edwin@k7>ftp myownmachine

ftp: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
220 myownmachine FTP server (Version 6.00LS) ready.
331 Password required for myusername.
230 User myusername logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> $ uploadtest

cd temp
250 CWD command successful.
put filename.tar.gz
local: filename.tar.gz remote: filename.tar.gz
150 Opening BINARY mode data connection for 'filename.tar.gz'
100% |**************************************************|  1103       00:00 ETA
226 Transfer complete.
1103 bytes sent in 0.01 seconds (215.00 KB/s)
quit
221 Goodbye.

...or from on the command-line:

[~] edwin@k7>echo "\$ uploadtest" | ftp myownmachine

ftp: connect to address ::1: Connection refused
Trying 127.0.0.1...
100% |**************************************************|  1103       00:00 ETA

There is not much information here, because ftp doesn't expect a terminal here. If you use ftp -v , there will be more output.

An example with arguments is

[~] edwin@k7>echo "\$ dailyupload `date +'%Y%m%d'`"

$ dailyupload 20010827
[~] edwin@k7>echo "\$ dailyupload `date +'%Y%m%d'`" | ftp myownmachine

ftp: connect to address ::1: Connection refused
Trying 127.0.0.1...
100% |**************************************************|  1103       00:00 ETA

It will upload the file daily-20010827.tar.gz.

Debugging of ftp-sessions

What can go wrong

The only problems I've encountered are

  • People forgetting to add an empty line after the last macdef statement.
  • People with incorrect permissions on the .netrc-file.

Debugging

  • If you are running the macro via the commandline, use the -v option to see more output.
  • If you run ftp with the -d option you will get all commands send to the ftpd.
分享到:
评论

相关推荐

    cmd操作命令和linux命令大全收集

    ftp ip 端口 用于上传文件至服务器或进行文件操作,默认端口为21。bin指用二进制方式传送(可执行文件进);默认为ASCII格式传送(文本文件时) route print 显示出IP路由,将主要显示网络地址Network addres,子网...

    rubygem-netrc-doc-0.11.0-2.el7.noarch.rpm

    官方离线安装包,亲测可用

    FTP命令是Internet用户使用最频繁的命令之一FTP命令详解

    FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作 系统下使用FTP,都会遇到大量的FTP内部命令,熟悉并灵活应用FTP的内部命令,...-n 限制ftp的自动登录,即不使用.netrc文件。 -g 取消全局文件名。

    node-netrc:用于读取.netrc文件的Node.js模块

    一个Node.js模块,用于读取您的.netrc文件。 因此,您不必在所有正在编写的Node.js CLI命令中都输入密码。 安装 npm install https://github.com/jazzdev/node-netrc.git 用法 var netrc = require('netrc'); var ...

    sclo-vagrant1-rubygem-netrc-doc-0.7.7-8.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    curl命令详解.pdf

    --netrc-optional 使用.netrc 或者URL 来覆盖-n --ntlm 使用HTTP NTLM 身份验证 -N/--no-buffer 禁用缓冲输出 -o/--output 把输出写到该文件中 -O/--remote-name 把输出写到该文件中,保留远程文件的文件名 -p/--...

    netrc:解析.netrc

    网络一个小型图书馆,可以帮助您处理.netrc文件。也可以看看

    netrc:Golang NetRC解析器

    Go的netrc解析器。 用法 获取主机的凭据。 usr , err := user . Current () n , err := netrc . Parse ( filepath . Join ( usr . HomeDir , ".netrc" )) fmt . Println ( n . Machine ( "api.heroku.com" ). Get ...

    perl6-Config-Netrc:.netrc用于Perl 6的文件解析器模块

    Config :: Netrc-用于解析Netrc配置文件的模块。 概要 use Config::Netrc; say Config::Netrc::parse-file( ' my-example.netrc ' ); 描述 基本上有两个主要功能:解析和解析文件。 第一个函数采用一串netrc...

    ftp自动上传脚本分享

    = 2 ] then echo “Usage: ftp-put.sh binary 192.192.4.101” exitfiftp -n<<! # -n: 不受.netrc文件的影响,<< : 使用即时文件重定向输入open $2 # para2 : 192.192.4.101user root root # ...

    CIS_Solaris_10_Benchmark_v5.0.0

    Table of Contents Table of Contents ....................................................................................................................................................

    octokit-auth-netrc:用于Octokit的netrc auth插件

    octokit-auth-netrc 适用于 auth插件 目录 用法 安装 $ npm install octokit-auth-netrc --save-prod 定义令牌 将添加到您的 machine api.github.com login 例子 进口 import { createNetrcAuth } from 'octokit-...

    netrc:读写netrc文件

    原料药读取一个netrc文件: n = Netrc.read("sample.netrc")如果该文件不存在,则Netrc.read将返回一个空对象。 如果文件名以“ .gpg”结尾,它将使用解密。 读取用户的默认netrc文件。 在Unix上: $NETRC/.netrc或$...

    tropess-tools-idl:如何使用IDL的TROPESS产品

    echo ' machine urs.earthdata.nasa.gov login <uid> password <password> ' >> .netrc 创建一个cookie文件。该文件将用于在对wget的调用之间保留会话。 cd ~ touch .urs_cookies 注意:如果您已经执行了wget而...

    mapbox-core-maps-ios

    将以下内容插入或附加到~/.netrc machine api.mapbox.com login mapbox password 一体化 迅捷软件包管理器 使用SPM软件包 :mobile_phone: :desktop_computer: :laptop: . package ( url : " git@github....

    rubygem-netrc-0.11.0-2.el7.noarch.rpm

    官方离线安装包,亲测可用

    sclo-vagrant1-rubygem-netrc-0.7.7-8.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    netrc2:实用程序,用于解析和保存netrc文件

    netrc2 读取和保存文件的简单实用程序。 为什么要使用netrc2? 因为“ netrc”已在npm上使用。 安装 npm install netrc2 --save 用法 注意:netrc2是一个同步模块。 var netrc = require ( 'netrc2' ) ; var ...

    python3.6.5参考手册 chm

    ftp popen select gzip and zipfile tarfile hashlib ast os shutil sqlite3 html socket ssl nntp certificates imaplib http.client unittest random poplib asyncore tempfile inspect ...

    davpush:将文件推送到 WebDAV 服务器

    Cadaver支持通过 .netrc 文件自动登录需要身份验证的服务器,例如ftp客户端。 语法是这样的,文件可以为两个工具共享。 文件 ~/.netrc 可用于自动登录到需要身份验证的服务器。 可以使用以下标记(由空格、制表符...

Global site tag (gtag.js) - Google Analytics