通过python在脚本中使用密码ssh到远程机器 [英] ssh to remote machine with password in script via python

查看:61
本文介绍了通过python在脚本中使用密码ssh到远程机器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用远程机器.每次我需要验证文件更新时间时,我都必须 ssh,并且有多个脚本可以对远程机器执行 ssh.我查看了互联网,但无法根据我的要求找到.

I am working with remote machine. I had to ssh everytime i need verification of file update time and there are multiple scripts which will do ssh to the remote machine. I look over internet but couldn't find according to my requirements.

我试图找到一个使用 ssh 的 python 脚本,密码也在脚本中,因为我的 python 脚本将每 5 分钟检查一次文件修改时间,并且每次脚本执行时我都无法输入密码.我从 SO 和互联网上尝试了这些代码,但无法满足我的需求.使用python脚本提供密码建立ssh会话

I am trying to find a python script which uses ssh and the password is also in the script because my python scripts will check every 5 minutes the file modification times and i cannot enter password everytime the script execute. I tried these codes from SO and internet but couldn't fulfil my need. Establish ssh session by giving password using script in python

如何使用python远程执行进程

我也简单地通过 ssh 进入一台远程机器.然后我试图 ssh 但通过 python 脚本到另一台远程机器 cox 这个 python 脚本还包括检查不同文件修改时间的代码..意味着我已经 ssh 到一台远程机器,然后我想从那里运行一些 python 脚本,检查另一台远程机器上文件的文件修改时间.有没有一种简单的方法来 ssh 远程机器以及 python 脚本中的密码..我将不胜感激.

Also I enter into one remote machine through ssh simply.then I am trying to ssh but via python script to another remote machine cox this python script also include code to check the modification time of different files.. mean i already ssh to one remote machine and then i want to run some python scripts from there which checks file modification time of files on another remote machine. Is there a simple way to ssh remote machine along with password in python script. . I would be grateful.

推荐答案

如果你想尝试 paramiko 模块.这是一个示例工作 python 脚本.

If you want to try paramiko module. Here is a sample working python script.

import paramiko

def start_connection():
    u_name = 'root'
    pswd = ''
    port = 22
    r_ip = '198.x.x.x'
    sec_key = '/mycert.ppk'

    myconn = paramiko.SSHClient()
    myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    my_rsa_key = paramiko.RSAKey.from_private_key_file(sec_key)

    session = myconn.connect(r_ip, username =u_name, password=pswd, port=port,pkey=my_rsa_key)

    remote_cmd = 'ifconfig'
    (stdin, stdout, stderr) = myconn.exec_command(remote_cmd)
    print("{}".format(stdout.read()))
    print("{}".format(type(myconn)))
    print("Options available to deal with the connectios are many like\n{}".format(dir(myconn)))
    myconn.close()


if __name__ == '__main__':
    start_connection()

这篇关于通过python在脚本中使用密码ssh到远程机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆