使用 Junos PyEZ 传输文件
Junos PyEZ 提供实用程序,使您能够在 Junos 设备上执行文件管理任务。您可以使用 Junos PyEZ jnpr.junos.utils.scp.SCP
类在本地主机和 Junos 设备之间保护复制 (SCP) 文件。
这些SCP
open()
和close()
方法可以建立并终止与设备的连接。因此,如果客户端应用程序仅执行文件复制操作,则可以省略对和close()
方法的Device
open()
呼叫。该类的SCP
实例可用作上下文管理器,自动调用open()
和close()
方法。例如:
from jnpr.junos import Device from jnpr.junos.utils.scp import SCP dev = Device('router1.example.com') with SCP(dev) as scp: scp.put('local-file', remote_path='path') scp.get('remote-file', local_path='path')
SCP
使您能够使用 progress
参数跟踪传输进度。默认情况下, SCP
不会打印进度消息。设置 progress=True
为以 10% 或更高的传输完成间隔打印默认进度消息。
with SCP(dev, progress=True) as scp:
或者,您可以定义自定义功能来打印进度消息,然后设置 progress
等于该功能名称的参数。功能定义应包含与设备实例和进度消息对应的两个参数。例如:
def log(dev, report): print (dev.hostname + ': ' + report) def main(): ... with SCP(dev, progress=log) as scp:
以下示例程序会将 scp-test1.txt 和 scp-test2.txt 文件从本地主机传输到目标设备上的 /var/tmp 目录,然后将消息日志文件从目标设备传输到本地主机上 的日志 目录。消息日志将更名为将设备主机名附加到文件名。该示例使用已在本地主机上和设备上配置的 SSH 密钥进行身份验证。
为了进行比较,程序同时使用默认进度消息和在名为的 log
功能中定义的自定义消息来跟踪传输的进展。
from jnpr.junos import Device from jnpr.junos.utils.scp import SCP def log(dev, report): print (dev.hostname + ': ' + report) def main(): dev = Device('router1.example.com') msgfile = 'logs/'+dev.hostname+'-messages' try: #Default progress messages with SCP(dev, progress=True) as scp1: scp1.put('scp-test1.txt', remote_path='/var/tmp/') scp1.get('/var/log/messages', local_path=msgfile) #Custom progress messages with SCP(dev, progress=log) as scp2: scp2.put('scp-test2.txt', remote_path='/var/tmp/') scp2.get('/var/log/messages', local_path=msgfile) except Exception as err: print (err) return if __name__ == "__main__": main()
传输进度发送至标准输出。默认输出 (progress=True
) 包括设备名称、正在传输的文件以及传输进度(以两个字节和百分比为百分比)。
router1.example.com: scp-test1.txt: 8 / 8 (100%) router1.example.com: logs/router1.example.com-messages: 0 / 229513 (0%) router1.example.com: logs/router1.example.com-messages: 24576 / 229513 (10%) router1.example.com: logs/router1.example.com-messages: 139264 / 229513 (60%) router1.example.com: logs/router1.example.com-messages: 229513 / 229513 (100%)
在此情况下,自定义功能会产生类似的输出。
router1.example.com : scp-test2.txt: 1 / 1 (100%) router1.example.com : logs/router1.example.com-messages: 0 / 526493 (0%) router1.example.com : logs/router1.example.com-messages: 57344 / 526493 (10%) router1.example.com : logs/router1.example.com-messages: 106496 / 526493 (20%) router1.example.com : logs/router1.example.com-messages: 212992 / 526493 (40%) router1.example.com : logs/router1.example.com-messages: 319488 / 526493 (60%) router1.example.com : logs/router1.example.com-messages: 368640 / 526493 (70%) router1.example.com : logs/router1.example.com-messages: 475136 / 526493 (90%) router1.example.com : logs/router1.example.com-messages: 526493 / 526493 (100%)
执行程序后,在目标设备上发出 file list
命令,验证 scp-test1.txt 和 scp-test2.txt 文件是否被复制到正确的目录中。
user1@router1> file list /var/tmp/scp-test* /var/tmp/scp-test1.txt /var/tmp/scp-test2.txt
在本地主机上,应在日志目录中显示消息日志文件,该文件已重命名为包含设备主机名。
[user1@server ~]$ ls logs router1.example.com-messages
默认情况下,Junos PyEZ 会在 ~/.ssh/config(如果存在)查询默认 SSH 配置文件。但是,在创建设备实例时,可以通过在参数列表中Device
包括ssh_config
参数来指定不同的 SSH 配置文件。例如:
ssh_config_file = '~/.ssh/config_dc' dev = Device('198.51.100.1', ssh_config=ssh_config_file)
此外,当在参数列表中Device
包括ssh_private_key_file
用于定义特定 SSH 私有密钥文件以进行身份验证的参数时,SCP
实例在传输文件时使用相同的密钥文件进行身份验证。
key_file='/home/user1/.ssh/id_rsa_dc' dev = Device('198.51.100.1', ssh_private_key_file=key_file) with SCP(dev) as scp: scp.put('scp-test.txt', remote_path='/var/tmp/')
该 SCP
类还支持代理通信,使您能够通过支持 netcat 的中介主机将文件从本地主机传输到目标设备。只能通过中间主机登录目标设备时,这很有用。要配置代理通信,请将相应信息添加至 SSH 配置文件。例如:
[user1@server ~]$ cat ~/.ssh/config Host 198.51.100.1 User user1 ProxyCommand ssh -l user1 198.51.100.2 nc %h 22 2>/dev/null