简体   繁体   English

脚本自动SSH到服务器

[英]Script to ssh into server automatically

I'm trying to create a script to automatically log me in to a password protected server to upload a file from an ubuntu directory automatically. 我正在尝试创建一个脚本,以自动将我登录到受密码保护的服务器,以自动从ubuntu目录上载文件。 I am doing this for the purposes of automatically backing up a directory every hour. 我这样做是为了每小时自动备份一个目录。 I already know had to add cron jobs to run my script every hour, but I don't know how to SSH, zip my directory then upload it. 我已经知道必须添加cron作业才能每小时运行我的脚本,但是我不知道如何SSH,压缩目录然后上载它。 I don't know bash scripting very well, and in fact I was thinking it might be easier for me to use a python script. 我不太了解bash脚本,实际上我在想使用python脚本可能更容易。 What is the best way to go about doing this? 这样做的最佳方法是什么?

You absolutely need public keys instead of passwords. 您绝对需要公用密钥而不是密码。 And using bash will be far easier than python. 而且使用bash比python容易得多。 Simply zip your file and then scp it or rsync the whole dir. 简单地zip文件,然后scp ,或rsync整个目录。

You could do this with Rsync . 您可以使用Rsync做到这一点。 It supports ssh tunneling and is a very good tool for backups. 它支持ssh隧道传输,是备份的很好的工具。

zip -R backup.zip directory && scp backup.zip username@server:destination_path

should do the job. 应该做的工作。 As stated above, you should definitely use key authentication for this. 如上所述,您绝对应该为此使用密钥身份验证。

You can try to use "Twisted conch library" 您可以尝试使用“ Twisted海螺库”

Conch is an SSHv2 implementation written in Python. Conch是用Python编写的SSHv2实现。

There is an example here: 这里有一个例子:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM