远程登录Linux服务器修改ssh端口
公司有部分服务器root密码被禁用,有部分没有禁用,禁用root的服务器需要通过tomcat用户登陆系统,切换至root修改端口,没有禁用的直接修改root密码;
#-*- coding:utf-8 -*-
import paramiko
import time
'''远程登录修改端口'''
def ssh_connect( _host, _username, _password ):
try:
_ssh_fd = paramiko.SSHClient()
_ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
_ssh_fd.connect( _host, username = _username, password = _password )
except Exception as e:
return ('ssh %s@%s: %s' % (_username, _host, e))
return _ssh_fd def ssh_exec_cmd( _ssh_fd, _cmd ):
return _ssh_fd.exec_command( _cmd ) def remote_ssh(_ssh_fd,cmd,root_pwd):
ssh = _ssh_fd.invoke_shell()
time.sleep(0.1)
ssh.send('su - \n')
buff = ''
while not buff.endswith('Password: '):
resp = ssh.recv(9999)
buff +=resp
ssh.send(root_pwd)
ssh.send('\n')
buff = ''
while not buff.endswith('# '):
resp = ssh.recv(9999)
buff +=resp
ssh.send(cmd)
ssh.send('\n')
buff = ''
while not buff.endswith('# '):
resp = ssh.recv(9999)
buff +=resp
_ssh_fd.close()
result = buff
return result
def ssh_close( _ssh_fd ):
_ssh_fd.close() def main():
hostname = ['xxx.xx.xx.xx']
username = ['root','tomcat']
password = ['********','*********']
cmd = 'echo "Port 61822" >> /etc/ssh/sshd_config && service sshd restart'
for i in range(len(hostname)):
sshd = ssh_connect( hostname[i] , username[0] , password[0])
if "Authentication failed" in str(sshd):
print 'PermitRootLogin no'
sshd = ssh_connect( hostname[i] , username[1] , password[1])
print remote_ssh(sshd,cmd,password[0])
else:
print 'PermitRootLogin yes'
stdin, stdout, stderr = ssh_exec_cmd( sshd, cmd )
stdout_list = stdout.readlines()
print stdout_list if __name__ == "__main__":
# pass
main()
远程登录Linux服务器修改ssh端口的更多相关文章
- ssh远程登录linux服务器
ssh远程登录linux服务器 用法: ssh -l user -p port server_ip 或者 ssh -p port user@server_ip 参数: -l 后接要登录的远程系统用户名 ...
- ZH奶酪:putty远程登录Linux服务器非常慢
11.pytty远程登录Linux服务器非常慢 http://www.it165.net/os/html/201209/3425.html 12.启动SSHD服务报错 http://blog.chin ...
- 搭建高性能计算环境(二)、远程登录Linux服务器
一般操作Linux系统都是通过远程登录使用的,本节介绍几种远程登录Linux.上传下载文件的工具. 1. Secure Shell SSH 简单方便.既能使用命令行登陆也能传文件,免费. 打开SSH ...
- 自动拒绝恶意IP远程登录Linux服务器脚本
当我们已经配置了iptables防火墙,我们允许22端口对外网所有人访问,当然这也是为了方便,我们在任何地方都连接上,没有做VPN,也没有做ssh密钥验证,但是我们的密码设置得非常复杂,大小写.特殊符 ...
- 恶意IP远程登录Linux服务器脚本
#!/bin/sh #auto drop ssh failed IP address #定义变量 SEC_FILE=/var/log/secure #如下为截取secure文件恶意ip 远程登录22端 ...
- “通过jumpserver远程登录linux服务器,rz上传文件速度过慢”问题的解决
问题: windows通过jumpserver远程登录到linux服务器,使用rz上传jar包,速度太慢(10k以内). 解决方案: 思路:通过ssh直接登录远程服务器 1.secureCRT-> ...
- 利用VNC远程登录Linux服务器简易版
我负责管理实验室的一台服务器,安装的系统是CentOS 6.7.使用pietty远程登录服务器(命令行) 需求:使实验室的同学和老师使用RealVNC远程登录服务器. 一,首先检查一下服务器是否安装V ...
- linux服务器修改ssh默认22端口方法
1.登录服务器,打开sshd_config文件 # vim /etc/ssh/sshd_config 2.找到#Port 22,默认是注释掉的,先把前面的#号去掉,再插入一行设置成你想要的端口号,注意 ...
- 实现移动设备远程登录linux服务器
明天下午要考试了.所以最近时间有点紧,都在忙着看矩阵课本,刷刷题. 回到我们博客题目内容,最近正在看linux,之前是通过在笔记本上装着虚拟机的方法来进行操作. 之后发现一些能够进一步改进的地方,首先 ...
随机推荐
- 老李分享:https协议
老李分享:https协议 最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如果你访问 google,查看一下地址栏 ...
- 第一个python爬虫程序
1.安装Python环境 官网https://www.python.org/下载与操作系统匹配的安装程序,安装并配置环境变量 2.IntelliJ Idea安装Python插件 我用的idea,在工具 ...
- SpringMVC基础学习(一)—初识SpringMVC
一.HelloWorld 1.导入SpringMVC所需的jar包 2.配置web.xml 配置DispatcherServlet.DispatcherServlet默认加载/ ...
- Redis数据类型之字符串String
String类型是Redis中最基本也最简单的一种数据类型 首先演示一些常用的命令 一.SET key value 和GET key SET key value 和 GET key 设置键值和获取值 ...
- MYSQL PGA SGA设置
mysql使用总内存 = global_buffers + thread_buffers All thread buffer(会话/线程级内存分配总和) = max_threads(当前活跃连接数) ...
- 使用react native制作的一款网络音乐播放器
使用react native制作的一款网络音乐播放器 基于第三方库 react-native-video设计"react-native-video": "^1.0.0&q ...
- CI Weekly #17 | flow.ci 支持 Java 构建以及 Docker/DevOps 实践分享
这周一,我们迫不及待写下了最新的 changelog -- 项目语言新增「Java」.创建 Java 项目工作流和其它语言项目配置很相似,flow.ci 提供了默认的 Java 项目构建流程模版,快去 ...
- vscode奇淫记(上)
每次换editor都是一种煎熬,从最早的eclipse,sublime,webstorm到现在在用的atom,换编辑器的驱动是寻找更酷炫和轻量的平衡点,其实我真的蛮喜欢atom的,酷炫!那我这次打算入 ...
- PHP7中我们应该学习会用的新特性
PHP7于2015年11月正式发布,本次更新可谓是PHP的重要里程碑,它将带来显著的性能改进和新特性,并对之前版本的一些特性进行改进.本文小编将和大家一起来了解探讨PHP7中的新特性. 1. 标量类型 ...
- jQuery扩展函数设置所有对象只读
jQuery(function ($) { $.fn.disable = function () { return this.each(func ...