python ssh脚本错误

时间:2013-02-08 21:05:09

标签: python ssh ssh-keys

只是一些背景知识,我正在编写一个脚本来ssh到几个远程服务器并将我的公钥复制到它们。我开始编写一个custon ssh客户端,因为我无法安装任何外部python模块。长话短说,我工作的公司说,如果它不是标准python库的一部分,你就不能使用它。

经过数周的搜索,我找到了一个我认为是我所有祈祷的答案的剧本。我已经对它进行了修改以满足我的需求,但我发现了一个未找到变量的错误,我需要一些指导来解决它。

这是脚本:

#!/usr/bin/python

import pty, re, os, sys, stat, socket
from getpass import getpass

class ssherror(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class sshclient:
    def __init__(self, ip, passwd, user, port):
        self.ip = ip
        self.passwd = passwd
        self.user = user
        self.port = port

    def runcmd(self, c):
        (pid, f) = pyt.fork()
        if pid == 0:
            os.execlp("ssh", "ssh", '-p %d' % self.port, self.user + '@' + self.ip, c)
        else:
        return (pid, f)

    def pushfile(self, src, dst):
        (pid, f) = pty.fork()
        if pid == 0:
            os.execlp("scp", "scp", '-P %d' % self.port, src, self.user + '@' + self.ip + ':' + dst)
        else:
            return (pid, f)

    def pushdir(self, src, dst):
        (pid, f) = pty.fork()
        if pid == 0:
            os.execlp("scp", "scp", '-P %d' % self.port, "-r", src, self.user + '@' + self.ip + ':' + dst)
        else:
            return (pid, f)

    def _read(self, f):
        x = ''
        try:
            x = os.read(f, 1024)
        except Exception, e:
            pass
        return x

    def connect(self, pid, f):
        output = ''
        got = self._read(f)
        m = re.search('authenticity of host', got)
        if m:
            os.write(f, 'yes\n')
            while True:
                got = self._read(f)
                m = re.search('Permanently added', got)
                if m:
                    break

            got = self._read(f)
        m = re.search('assword:', got)
        if m:
            os.write(f, self.passwd + '\n')
            tmp = self._read(f)
            tmp += self._read(f)
            m = re.search('Permission denied', tmp)
            if m:
                raise Exception('Invalid passwd')
            got = tmp
        while got and len(got) > 0:
            output += got
            got = self._read(f)
        os.waitpid(pid, 0)
        os.close(f)
        return output

    def cmd(self, c):
        (pid, f) = self.runcmd(c)
        return self.connect(pid, f)

    def push(self, src, dst):
        s = os.stat(src)
        if stat.S_ISDIR(s[stat.ST_MODE]):
            (pid, f) = self.pushdir(src, dst)
        else:
            (pid, f) = self.pushfile(src, dst)
        return self.connect(pid, f)



def sshconnect(ip, passwd, user, port=22):
    s = sshclient(ip, passwd, user, port)
    return s.connect(pid, f)


def sshpush(ip, passwd, src, dst, user, port=22):
    s = sshclient(ip, passwd, user, port)
    return s.push(src, dst)



passwd = getpass()
src = '/path/to/dir/being/copied'
dst = '/path/to/destination/directory'
user = os.getlogin()
port = portnum
hosts = ['host1', 'host2', 'host3']
for host in hosts:
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, msg:
        print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
        sys.exit()

    print 'Socket Created'
    try:
        ip = socket.gethostbyname(host)
    except socket.gaierror:
        print 'Hostname could not be resolved. Exiting...'
        sys.exit()

    print 'Ip address of ' + host + ' is ' + ip
    sshconnect(ip, passwd, user, port)
    sshpush(ip, passwd, src, dst, user, port=22)
    print 'ssh directory copied to ' + host

我对它做了很多修改,但我仍然收到以下错误:

Traceback (most recent call last):
  File "ssh.py", line 129, in <module>
    sshconnect(ip, passwd, user, port)
  File "ssh.py", line 99, in sshconnect
    return s.connect(pid, f)
NameError: global name 'pid' is not defined

我知道我需要定义pid,但我在最佳方式上做了一个空白。任何帮助,建议,Advil将是最诚挚的赞赏!!!

1 个答案:

答案 0 :(得分:1)

Typos on line:

20:(pid, f) = pyt.fork()(应该是pty)

24:else:

后没有任何内容

93:pidf未在此功能范围中定义 - &gt;这是你的问题。

106:portnum在此范围内未知

他们是否允许您使用proper IDE进行代码验证?切换到它。 否则,如果你喜欢它,那么传统的静态来源checker

查看Paul Mikesell的代码片段我认为您不需要第91行的sshconnect功能。只需拨打sshclient.push,就像您在下一个功能中所做的那样({{1它将连接自己。