简体   繁体   中英

How do I switch user in Linux using ruby and net/ssh?

Using below function I can connect to my Linux machine. But few commands need root permission to execute and direct root login is disabled. Also, the user cannot sudo.

require 'net/ssh'
def sshutm(host,un,pwd,cmd)
  Net::SSH.start( host, un, :password => pwd ) do|ssh|
    result = ssh.exec!(cmd)
    return result
  end
end

I tried this but it dint work.

def sshutm(host,un,pwd,cmd)
  Net::SSH.start( host, un, :password => pwd ) do|ssh|
    ssh.exec!("su")
    ssh.exec!("passowrd")
    result = ssh.exec!(cmd)
    return result
  end
end

You could make your user a sudo user (eg http://www.linux.com/learn/tutorials/306766:linux-101-introduction-to-sudo ), and execute the given command with sudo (eg sudo ls /root ). Details vary on the used distribution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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