简体   繁体   English

通过在Ruby中使用net :: ssh使用sudo命令从远程系统获取信息

[英]get the information from the remote system using sudo command by using net::ssh in ruby

I have a situation as i have to login to remote system and get the their hardware information. 我有一种情况,因为我必须登录到远程系统并获取其硬件信息。 I logged in to their system i have used ssh for this i am using net-ssh gem. 我登录到他们的系统,为此我已经使用ssh了,而我正在使用net-ssh gem。 here is my code where i logged into and get the info 这是我登录并获取信息的代码

Net::SSH.start('host','user', :password => 'xxxxxx') do |ssh|
    ssh.exec!("echo 'xxxxxx' | sudo -S dmidecode -s system-serial-number")
end 

for getting the information I have used sudo -S dmidecode -s system-serial-number command it is giving the output with password for user as password for user 1.0.0 how can i remove that message? 为了获得我使用过的信息sudo -S dmidecode -s system-serial-number命令,它为用户提供带密码的输出作为用户1.0.0的密码,我如何删除该消息?

Ihave refer this link but i also implemented it but it will taking to much time for loading how is their any other way to do this? 我已经引用了此链接,但我也实现了该链接,但是加载将花费很多时间,他们的其他方法是怎么做到的呢?

    result = nil
    session.exec!("sudo -S dmidecode -s system-serial-number") do |channel, stream, data|
    if data =~ /^\[sudo\] password for user:/
        channel.send_data 'your_sudo_password'
    else
        result << data
    end
    result

d d

sudo intentionally tries to prevent this to increase security. sudo故意尝试阻止这种情况以提高安全性。 The ideal solution is to add a NOPASSWD option in the sudoers file, for the commands you need, to the username being used to create the ssh connection. 理想的解决方案是将sudoers文件中的NOPASSWD选项(用于所需命令)添加到用于创建ssh连接的用户名。 eg: THESSHUSER ALL = NOPASSWD: dmidecode, system-serial-number 例如:THESSHUSER ALL = NOPASSWD:dmidecode,系统序列号

However, replace the commands w/their full absolute paths. 但是,请替换带有完整绝对路径的命令。 Also, don't just give the ssh user NOPASSWD for all commands, restrict it to the commands you want to run, otherwise you are lowering the system's security. 另外,不要只给ssh用户NOPASSWD提供所有命令,而是将其限制为您要运行的命令,否则会降低系统的安全性。 The sudoers file is usually in /etc/sudoers and is edited w/a special command on many machines, that command is visudo, which you must run as root (ie with sudo ;). sudoers文件通常位于/ etc / sudoers中,并且在许多机器上使用特殊命令进行编辑,该命令为visudo,您必须以root用户身份运行(即sudo;)。 Once you have edited the sudoers file you can run these commands w/o being prompted for a password. 一旦编辑了sudoers文件,就可以运行这些命令而无需提示输入密码。

Not exactly sure what the problem is, but perhaps you want to try and use the -p switch to change the password prompt to an empty string: 不能完全确定问题是什么,但是也许您想尝试使用-p开关将密码提示更改为空字符串:

ssh.exec!("echo 'xxxxxx' | sudo -p '' -S dmidecode -s system-serial-number")
 -p The -p (prompt) option allows you to override the default password prompt and use a custom one. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM