`
isiqi
  • 浏览: 16340654 次
  • 性别: 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,子网...

    即用即查Linux命令行实例参考手册 1

    - 实例1:从Linux系统通过`root`用户登录至Windows FTP服务器: 1. 在Linux主机上运行`ftp 10.0.101.5`。 2. 使用`pwd`查看远程当前目录,使用`ls`列出目录内容。 3. 使用`get`下载文件,例如`get user-guide....

    gamit10.71自动下载数据解决步骤

    标题“gamit10.71自动下载数据解决步骤”涉及到的是使用Gamit软件进行自动数据下载的问题。Gamit(Geodetic Analysis and Modeling Integrated Toolkit)是一款广泛应用于地球科学领域的开源软件,主要用于全球定位...

    FTP命令大全

    - `-n`:限制自动登录,防止使用netrc文件自动保存登录信息。 - `-g`:取消全局文件名解析,防止对路径进行特殊处理。 以下是FTP常用的一些内部命令: 1. `!cmd[args>`:在本地执行shell命令,`exit`返回FTP环境。...

    FTP 命令详解

    - `-n`:禁止自动登录和使用`.netrc`文件。 - `-d`:开启调试模式。 - `-g`:取消全局文件名。 熟悉并熟练使用这些FTP命令,可以极大地提高在不同操作系统环境下进行文件传输的效率和便利性。通过FTP,用户可以...

    linxu通过ftp备份

    为了能够通过FTP传输备份文件,需要先设置好FTP客户端的配置文件(如`.netrc`),其中包含远程服务器的登录信息以及本地和远程目录的路径: ```bash # .netrc文件内容 machine 192.168.0.111 login oracle password...

    Oracle自动备份

    - 创建 `.netrc` 文件以自动登录到FTP服务器。 ```bash machine <ftp_server_ip> login oracle password <ftp_password> macdef init bin lcd /backup cd /pub mput * bye ``` - 设置 `.netrc` 的权限...

    sh代码-curl auth ter global

    `curl`在Linux、Unix-like系统以及Windows广泛使用,是开发者和系统管理员日常工作中必备的工具之一。本文将深入探讨如何使用`curl`进行基本的身份验证和全局配置。 标题“sh代码-curl auth ter global”可能指的是...

    lftp结合sftp文件传输.zip

    为了更安全地保存登录凭证,避免每次手动输入,可以创建一个名为`.netrc`的隐藏文件(在用户主目录下),并添加以下内容: ``` machine 服务器地址 login 用户名 password 密码 ``` 这样,lftp会自动读取这些信息...

    curl 实现SFTP上传和url Post Json信息

    在这个例子中,`-T` 参数指定要上传的本地文件,`sftp://` 后跟的是服务器地址和登录凭证。 如果不想在命令行中明文显示密码,可以创建一个 `.netrc` 文件存储认证信息: ```bash machine example.com login ...

Global site tag (gtag.js) - Google Analytics