Telnet to the Linux server to modify the SSH port

Source: Internet
Author: User
Tags ssh port

The company has some server root password is disabled, some are not disabled, disable the root of the server needs to go through the Tomcat user login system, switch to the root modification port, no disabled directly modify the root password;

#-*-coding:utf-8-*-ImportParamikoImport Time" "Remote Login Modify Port" "defSsh_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)exceptException as E:return('ssh%[email protected]%s:%s'%(_username, _host, e))return_SSH_FDdefssh_exec_cmd (_SSH_FD, _cmd):return_ssh_fd.exec_command (_cmd)defremote_ssh (_ssh_fd,cmd,root_pwd): SSH=_ssh_fd.invoke_shell () time.sleep (0.1) Ssh.send ('Su-\ n') Buff="'     while  notBuff.endswith ('Password:'): Resp= SSH.RECV (9999) Buff+=resp ssh.send (root_pwd) ssh.send ('\ n') Buff="'     while  notBuff.endswith ('# '): Resp= SSH.RECV (9999) Buff+=resp ssh.send (cmd) ssh.send ('\ n') Buff="'     while  notBuff.endswith ('# '): Resp= SSH.RECV (9999) Buff+=resp _ssh_fd.close () result=BuffreturnresultdefSsh_close (_SSH_FD): _ssh_fd.close ()defMain (): hostname= ['xxx.xx.xx.xx'] Username= ['Root','Tomcat'] Password= ['********','*********'] cmd='echo "Port 61822" >>/etc/ssh/sshd_config && service sshd Restart'     forIinchRange (LEN (hostname)): sshd=Ssh_connect (Hostname[i], username[0], password[0])if "Authentication failed" inchStr (sshd):Print 'Permitrootlogin No'sshd= Ssh_connect (Hostname[i], username[1], password[1])            Printremote_ssh (sshd,cmd,password[0])Else:            Print 'Permitrootlogin Yes'stdin, stdout, stderr=ssh_exec_cmd (sshd, cmd) stdout_list=Stdout.readlines ()Printstdout_listif __name__=="__main__":    #PassMain ()

Telnet to the Linux server to modify the SSH port

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.