LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-02-2012, 01:23 PM   #1
torchnw
LQ Newbie
 
Registered: Jun 2008
Distribution: Ubuntu, Debian, Mint, Fedora, Arch
Posts: 23

Rep: Reputation: 4
Python+paramiko - Can't create SSH connection when running as root


Hi,

I'm trying to create a python script to automate backup of networking device configurations over SSH. Using paramiko I can create the connection when running as a normal user ( which I added to group "root" to facilitate storing the output files under /var/log ), but when run as root, it fails to authenticate. The target device is a Cisco switch. These are the relevant bits of code:

Code:
class Connection(object):
    
    def __init__(self,  target,  username,  password):
        self.target = target
        self.username = username
        self.password = password
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # I've tried all of the following, but same result
        self.ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
	#self.ssh.load_host_keys("/root/.ssh/known_hosts")
	#self.ssh.load_system_host_keys()        
    
class Cisco_IOS(Connection):
    
    def fetch(self):
        self.ssh.connect(self.target, username=self.username, password=self.password, )
        channel = self.ssh.invoke_shell()
        channel.send('en\n')
        time.sleep(2)
        channel.send(self.password+"\n")
        time.sleep(2)
        flush = channel.recv(3000)
        print flush
        catch_error = re.compile("[Ee]rror")
	if catch_error.search(flush):
            log("Error authenticating to " + self.target +". Enable password different from login password")
        channel.send("terminal length 0\n")
	channel.send("show run\n")
        time.sleep(5)
	config = channel.recv(20000)
        print config
	return config
Now I believe it's somehow related to the user's and root's "~/.ssh/known_hosts" files, but I'm not sure how. The public keys are in both files, and also the line
Code:
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
should take care of adding the keys if they were missing. Permissions on both files are the same (644).

The error I'm getting is:
Quote:
File "./paratest.py", line 35, in fetch
self.ssh.connect(self.target, username=self.username, password=self.password, )
File "/usr/lib/python2.4/site-packages/paramiko/client.py", line 327, in connect
self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
File "/usr/lib/python2.4/site-packages/paramiko/client.py", line 481, in _auth
raise saved_exception
paramiko.AuthenticationException: Authentication failed.
As mentioned, I don't get this while running as a normal user, only as root. Any suggestions would be appreciated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paramiko and Python 2.6 ShaqDiesel Programming 1 03-03-2012 07:45 AM
How to create SSH user without access to root directory victorsk Linux - Networking 1 08-07-2009 06:56 PM
how can i create ssh connection bewteen my ipod touch and my pc deathalele Linux - Networking 1 01-05-2009 01:16 PM
Running a process decoupled from SSH connection? tim-f Linux - Newbie 4 07-10-2008 03:34 AM
running ssh connection into batch file jdonaldogg General 1 12-17-2005 10:25 PM


All times are GMT -5. The time now is 04:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration