I have currently written this script which works fine, but has a limitation,
I can't decide which source IP can open SSH with the server.
For routers with multiple interfaces it would be extremely useful.
I read that sock
or channel
could be used, but
I don't know how to implement them and I can't find examples.
Thanks!
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,)
Ok, now it works! Many thanks! My script finaly is:
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)