linux下使用脚本自动登录远程服务器
时间:2009-06-12 来源:uranusllj
ubuntu下的ssh客户端一直没找到象SecureCRT那样好用的工具。用ssh登录,每次都要输入密码,感觉很麻烦,而且因为字符集不相同,一般还有乱码问题,所以参照网上的一些实现自己也通过python来实现了一下。
#1.通过参数自动登录ssh远程服务器
#2.通过配置字符集解决中文乱码问题
#3.解决改变远程窗口默认较小bug,远程窗口同本地窗口大小动态改变未处理
#4.实现2级代理
#5.可以使用sftp登录来传文件(sftp,目前不能实现代理来作)
配置文件host.properties
第一列可以为id,第二列为用户名,第一列为密码,第四列为远程服务器字符集
#1.通过参数自动登录ssh远程服务器
#2.通过配置字符集解决中文乱码问题
#3.解决改变远程窗口默认较小bug,远程窗口同本地窗口大小动态改变未处理
#4.实现2级代理
#5.可以使用sftp登录来传文件(sftp,目前不能实现代理来作)
配置文件host.properties
第一列可以为id,第二列为用户名,第一列为密码,第四列为远程服务器字符集
Java代码
登录脚本
- #userid:user:password:characters
- ljj51:ljj:123456:gbk
登录脚本
Python代码
- #!/usr/bin/env python
- #coding:UTF-8
- #1.通过参数自动登录ssh远程服务器
- #2.通过配置字符集解决中文乱码问题
- #3.解决改变远程窗口默认较小bug,远程窗口同本地窗口大小动态改变未处理
- #4.实现2级代理
- import os,sys,pexpect,string
- import struct,fcntl,termios,signal
- try:
- import pexpect
- except:
- print "你没有pexpect包,试试sudo apt-get install python-pexpect"
- sys.exit(0)
- def sigwinch_passthrough(sig,data):
- """窗口改动后,自动返回当前窗口值,以便跟远程窗口同步,目前有问题"""
- s=struct.pack("HHHH",0,0,0,0)
- a=struct.pack('hhhh',fcntl.ioctl(sys.stdout.fileno(),termios.TIOCGWINSZ,s))
- #global columns,lines
- """ self.lines=a[0]
- self.columns=a[1]
- print "resize windows size(%s,%s)"%(self.lines,self.columns)
- """
- print a[0],a[1]
- class SimpleSsh:
- def __init__(self):
- self.columns=125
- self.lines=37
- self.base="~/ssh/"
- self.filename="host.properties"
- def createSshUrl(self,paramap):
- user=paramap["user"]
- password=paramap["password"]
- host=paramap["host"]
- characters=paramap["characters"]
- conntype=paramap["conntype"]
- url="luit -encoding "+characters+" "+conntype+" "+user+"@"+host
- print "#"*40
- print "#",string.center(string.join((user,"@",host," by ",characters),""),36),"#"
- print "#",string.center("created by [email protected]",36),"#"
- print "#"*40
- return url,password
- def connection(self,url,password):
- """ """
- columns=self.columns
- lines=self.lines
- try:
- p=pexpect.spawn(url)
- try:
- #signal.signal(signal.SIGWINCH,sigwinch_passthrough)
- p.setwinsize(lines,columns)
- print "resize windows(%s,%s)"%(columns,lines)
- except:
- pass
- p.expect("password:")
- p.sendline(password)
- p.interact()
- except:
- print "connection close()"
- def argv2map(self,userid,host,conntype):
- paramap={}
- base=self.base
- filename=self.filename
- tmp=""
- try:
- file=open(filename,"r")
- except:
- file=open(string.join(base,filename))
- content=file.readlines()
- for x in content:
- if x.startswith(userid):
- tmp=string.strip(x)
- if tmp=="":
- print "not found %s"%userid
- sys.exit(0)
- tmparray=string.split(tmp,":")
- paramap["user"]=tmparray[1]
- paramap["password"]=tmparray[2]
- paramap["userid"]=userid
- paramap["host"]=host
- paramap["characters"]=tmparray[3]
- paramap["conntype"]=conntype
- return paramap
- def initpara(self,para):
- try:
- userid=para[1]
- host=para[2]
- conntype=""
- except:
- userid=raw_input("enter your userid:")
- host=raw_input("enter your host:")
- try:
- conntype=para[3]
- except:
- conntype="ssh"
- return userid,host,conntype
- def handle(self,para):
- userid,host,conntype=self.initpara(para)
- paramap=self.argv2map(userid,host,conntype)
- url,password=self.createSshUrl(paramap)
- self.connection(url,password)
- class ProxySsh(SimpleSsh):
- def initpara(self,para):
- self.defaultType="ssh"
- paramaparray={}
- argvarray=string.split(para,"-")
- for x in argvarray:
- if string.strip(x)=="": continue
- x=string.split(x," ")
- #print x[1],",",x[2],",",self.defaultType
- paramaparray[x[0]]=self.argv2map(x[1],x[2],self.defaultType)
- return paramaparray
- def createSshUrl(self,paramaparray):
- parakey=paramaparray.keys()
- parakey.sort()
- url="luit -encoding %character2% ssh -t %s1% \"bash -c 'ssh %s2%'\""
- print "#"*40
- for x in parakey:
- paramap=paramaparray[x]
- tmp=string.join((paramap["user"],"@",paramap["host"]),"")
- tmp1="%s"+x+"%"
- tmp2="%character"+x+"%"
- print "#",string.center(string.join((x,":",tmp,paramap["characters"]),""),36),"#"
- url=string.replace(url,tmp1,tmp)
- url=string.replace(url,tmp2,paramap["characters"])
- print "#",string.center("created by [email protected]",36),"#"
- print "#"*40
- try:
- p=pexpect.spawn(url)
- try:
- p.setwinsize(self.lines,self.columns)
- print "resize windows(%s,%s)"%(columns,lines)
- except:
- pass
- for x in parakey:
- paramap=paramaparray[x]
- password=paramap["password"]
- p.expect("password")
- p.sendline(password)
- p.interact()
- except:
- print "connection close()"
- def handle(self,para):
- paramaparray=self.initpara(para)
- url=self.createSshUrl(paramaparray)
- if __name__=="__main__":
- """help:
- ./myssh.py ljj51 127.0.0.1
- ./myssh.py ljj51 127.0.0.1 sftp
- ./myssh.py -1 ljj51 127.0.0.1 -2 ljj52 127.0.0.2
- """
- help="""help:
- ./myssh.py ljj51 127.0.0.1
- ./myssh.py ljj51 127.0.0.1 sftp
- ./myssh.py -1 ljj51 127.0.0.1 -2 ljj52 127.0.0.2
- """
- argvpara=string.join(sys.argv[1:]," ")
- if "-" in argvpara:
- b=ProxySsh()
- b.handle(argvpara)
- elif "help" in argvpara:
- print help
相关阅读 更多 +