使用 Junos PyEZ 传输文件
总结 使用 Junos PyEZ 保护本地主机和 Junos 设备之间的复制 (SCP) 文件。
Junos PyEZ 提供的实用程序使您能够在 Junos 设备上执行文件管理任务。可以使用 Junos PyEZ jnpr.junos.utils.scp.SCP 类在本地主机和 Junos 设备之间保护复制 (SCP) 文件。
SCP open()和close()方法建立并终止与设备的连接。因此,如果客户端应用程序仅执行文件复制操作,则可以省略对 and Device open() close() 方法的调用。类的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
在本地主机上, 消息 日志文件(重命名以包含设备主机名)应存在于 logs 目录中。
user1@server:~$ ls logs router1.example.com-messages
默认情况下,Junos PyEZ 在 ~/.ssh/config 查询默认的 SSH 配置文件(如果存在)。但是,您可以在创建设备实例时通过在参数列表中包含 ssh_config 参数 Device 来指定不同的 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 类还提供对 ProxyCommand 的支持,该命令使您能够通过支持 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