只是有點背景,我正在編寫一個腳本來ssh到幾個遠程服務器,並將我的公鑰複製到他們。我着手編寫custon ssh客戶端,因爲我無法安裝任何外部python模塊。長話短說,我工作的公司說,如果它不是標準Python庫的一部分,你就不能使用它。python ssh腳本錯誤
經過數週的搜索,我發現一個腳本,我認爲這是我所有祈禱的答案。我已經修改它以適應我的需求,但是我得到了一個未找到的變量錯誤,我需要一些指導來解決它。
這裏的腳本:
#!/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將最誠摯的讚賞!後一無所獲else:
93:
說真的。那是什麼樣的公司? Python的最大優點之一是可用的包數量很多。 – RickyA 2013-02-08 21:09:44
您沒有名爲pid的var聲明,既沒有在函數中,也沒有作爲參數或全局變量。由於我不完全瞭解這種結構的原因,我不能幫助你進一步。 – 2013-02-08 21:11:04
RickyA--不要讓我開始!大聲笑我已經向他們提出了同樣的論點,直到我面對藍色,但當你有非技術人員作出技術決定,你進入這些類型的情況..... – 2013-02-11 15:43:54