我试图通过分配给键的多个值来迭代字典。像这样:
devices = {'device_ip':('10.1.1.1','10.1.1.2','10.1.1.3'),
'device_hostname':('sw1','sw2','sw3')}
然后我尝试将IP和Hostname值传递给类似的东西:
scp_command = 'sshpass -p 123 scp -o StrictHostKeyChecking=no cisco@' + devices['device_ip'] + ':startup.cfg ' + devices['device_hostname'] + filename
我不想要的是为每个IP /主机名对修改scp_command
,并重复使用此变量以使代码中的行数最少。
答案 0 :(得分:0)
如何为每个Here it's really simple to launch google maps from your application:
//geo:0,0 if you don't have lat/lng for the location and just wanna search using address.
Uri gmmIntentUri = Uri.parse("geo:0,0?q=pass the address here");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
//set package to "com.google.android.apps.maps" so that only google maps is opened.
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
对分配list
?
key-value
使用:devices = {'device_ip':['10.1.1.1','10.1.1.2','10.1.1.3'],
'device_hostname':['sw1','sw2','sw3']}
答案 1 :(得分:0)
正如我在评论中已经提到的,我会寻求不同的数据结构:
devices = {'sw1':'10.1.1.1',
'sw2':'10.1.1.2',
'sw3':'10.1.1.3'}
要构建scp_command
字符串,我建议使用str.format
,例如:
scp_command = 'sshpass -p 123 scp -o StrictHostKeyChecking=no cisco@{ip}:startup.cfg {host} {file}'
cfg_file='example.cfg'
for device in devices:
print(scp_command.format(ip=devices[device], host=device, file=cfg_file))
通过这种方式,您可以在字符串中引入占位符{}
,并将.format()
替换为您当前的数据。结果:
sshpass -p 123 scp -o StrictHostKeyChecking=no cisco@10.1.1.1:startup.cfg sw1 example.cfg
sshpass -p 123 scp -o StrictHostKeyChecking=no cisco@10.1.1.2:startup.cfg sw2 example.cfg
sshpass -p 123 scp -o StrictHostKeyChecking=no cisco@10.1.1.3:startup.cfg sw3 example.cfg