Question:
I'm trying to write a script to backup the cisco config via ssh and the paramiko module. The script itself:
import paramiko
import datetime
import sys
import os
import time
#now = datetime.datetime.now()
host = "192.168.100.1"
user = "user"
password = "password"
secret = "password"
port=22
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, username=user, password=password, port=port)
stdin, stdout, stderr = client.exec_command('enable')
stdin.write(secret +'\n')
stdin, stdout, stderr = client.exec_command('term len 0')
stdin, stdout, stderr = client.exec_command('sh ver')
a = stdout.read()
print a
f = open('testing.txt', 'a')
f.write(a)
f.close()
client.close()
on startup gives
C:\Python27>python.exe d:\cisco_backup_v0.1.py
SSH connection established to 192.168.100.1
Traceback (most recent call last):
File "d:\cisco_backup_v0.1.py", line 23, in <module>
stdin, stdout, stderr = client.exec_command('term len 0')
File "C:\Python27\lib\site-packages\paramiko\client.py", line 341, in exec_com
mand
chan = self._transport.open_session()
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 615, in open_
session
max_packet_size=max_packet_size)
File "C:\Python27\lib\site-packages\paramiko\transport.py", line 731, in open_
channel
raise e
EOFError
Version python 2.7 , running under Windows. If you comment out this line, then it swears further. I tried without enable password just remove sh ver
, then everything is fine.
Where is the mistake?
Answer:
Remade using client.invoke_shell()
Here is the script
import sys
import time
import paramiko
import os
import cmd
HOST = '192.168.100.1'
USER = 'user'
PASSWORD = 'password'
secret = 'password'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(HOST, username=USER, password=PASSWORD)
chan = client.invoke_shell()
time.sleep(1)
chan.send('en\n')
chan.send(secret +'\n')
time.sleep(1)
chan.send('term len 0\n')
time.sleep(1)
chan.send('sh run\n')
time.sleep(10)
output = chan.recv(99999)
time.sleep(3)
print output
time.sleep(10)
client.close()