Python 使用特定 IP 源的 Paramiko SSH 脚本
Python Paramiko SSH script that use specific IP source
我目前编写的这个脚本运行良好,但有一个限制,
我无法决定哪个源 IP 可以与服务器打开 SSH。
对于具有多个接口的路由器来说,这将非常有用。
我读到可以使用 sock
或 channel
,但是
我不知道如何实现它们,也找不到示例。
谢谢!
from junos import Junos_Context
import paramiko
from datetime import datetime
import jcs
user = Junos_Context['user-context']['login-name']
hostname = Junos_Context['hostname']
now = datetime.now()
day = now.strftime('%Y%m%d')
hour = now.strftime('%H%M%S')
#Sets up the ssh session and logs in as login "simone" with password "simone"
#to host '192.168.2.2'
host = '192.168.2.2'
login = 'simone'
passw = 'simone'
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=login, password=passw, look_for_keys=False, allow_agent=False)
chan = ssh.invoke_shell()
except:
print "Login to %s failed" % (host,)
chan = False
if chan:
sftp = ssh.open_sftp()
sftp.put('/config/juniper.conf.gz',
'/simone/backups/%s_%s_%s_%s_juniper.conf.gz' % (user,hostname,day,hour))
sftp.close()
ssh.close()
print "All it's OK %s ! " % (user,)
else:
print "Sorry, there is no connection to the host %s" % (host,)
使用 SSHClient.connect
to provide your customized socket bound to the source address of your choice. Based on Can Python select what network adapter when opening a socket? 的 sock
参数,应该这样做:
import socket
s = socks.socket()
s.bind((local_address, 0))
s.connect((host, 22))
ssh.connect(
host, username=login, password=passw, look_for_keys=False, allow_agent=False, sock=s)
好的,现在可以了!
非常感谢!
我的脚本最终是:
sok = socket.socket()
sok.bind((local_address, 0))
sok.connect((host, 22))
ssh.connect(host, username=login, password=passw, look_for_keys=False, allow_agent=False, sock=sok)
我目前编写的这个脚本运行良好,但有一个限制,
我无法决定哪个源 IP 可以与服务器打开 SSH。
对于具有多个接口的路由器来说,这将非常有用。
我读到可以使用 sock
或 channel
,但是
我不知道如何实现它们,也找不到示例。
谢谢!
from junos import Junos_Context
import paramiko
from datetime import datetime
import jcs
user = Junos_Context['user-context']['login-name']
hostname = Junos_Context['hostname']
now = datetime.now()
day = now.strftime('%Y%m%d')
hour = now.strftime('%H%M%S')
#Sets up the ssh session and logs in as login "simone" with password "simone"
#to host '192.168.2.2'
host = '192.168.2.2'
login = 'simone'
passw = 'simone'
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=login, password=passw, look_for_keys=False, allow_agent=False)
chan = ssh.invoke_shell()
except:
print "Login to %s failed" % (host,)
chan = False
if chan:
sftp = ssh.open_sftp()
sftp.put('/config/juniper.conf.gz',
'/simone/backups/%s_%s_%s_%s_juniper.conf.gz' % (user,hostname,day,hour))
sftp.close()
ssh.close()
print "All it's OK %s ! " % (user,)
else:
print "Sorry, there is no connection to the host %s" % (host,)
使用 SSHClient.connect
to provide your customized socket bound to the source address of your choice. Based on Can Python select what network adapter when opening a socket? 的 sock
参数,应该这样做:
import socket
s = socks.socket()
s.bind((local_address, 0))
s.connect((host, 22))
ssh.connect(
host, username=login, password=passw, look_for_keys=False, allow_agent=False, sock=s)
好的,现在可以了! 非常感谢! 我的脚本最终是:
sok = socket.socket() sok.bind((local_address, 0)) sok.connect((host, 22)) ssh.connect(host, username=login, password=passw, look_for_keys=False, allow_agent=False, sock=sok)