【问题标题】:SFTP to remote machines, switch to root and execute commands on remote machineSFTP到远程机器,切换到root并在远程机器上执行命令
【发布时间】:2017-11-28 09:05:37
【问题描述】:

我需要更新 10 多个远程主机上的文件。我们系统的设计方式是我们有一个用户说$user,它使用密码说 $passwd SSH 到任何远程主机,成功登录后,$user 只能在远程主机上运行一个命令,即sudo su - root(这个定义在/etc/sudoers)

我的问题是,我无法从我的脚本中sudo su - root,下面是我的代码

(注意 - 我已经尝试过 invoke_shell()get_transport()。)

import paramiko, re, os 
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
action_item = int(input("Welcome on-board ...\nPlease choose from following \n1 - Global Update\n2 - Specific Update\n Please input as 1 or 2 :"))
if action_item == 2:
    hostName = input("Please provide 'IPADDRESS' of Specific Remote Client :")
    os.system('touch /tmp/hosts_file_specific && umask 113 /tmp/hosts_file_specific')
    os.system('hostName='+hostName+' && echo "$hostName" >> /tmp/hosts_file_specific')
    hosts_file = r'/tmp/hosts_file_specific'
elif action_item == 1:
    hosts_file = r'/etc/hosts'
else:
    print("Invalid input, shutting down the application")
with open(hosts_file) as file:
    for line in file:
        line = re.findall( r'[0-9]+(?:\.[0-9]+){3}',line)
        if len(line) > 0:
            line = line[0]
        else:
            line = '127.0.0.1'
        if line == '127.0.0.1':
            pass
        else:
            print("Trying to connect - "+line)
            try:

                ssh_client.connect(hostname=line,username=$user,password=$passwd)
                stdin,stdout,stderr=ssh_client.exec_command("pgrep mttrapd")
                outData = stdout.readlines();
                outData = re.findall(r'[0-9]+',outData[0])
                outData = int(outData[0])
                print("[Probe = nco_p_mttrapd] and PID = ",outData)
            except TimeoutError:
                outData = -1
                pass
            if outData > 0:
                ftp_client = ssh_client.open_sftp()
                localfilepath1 = r"/tmp/Python_361/rules.tar"
                remotefilepath1 = r"/tmp/rules.tar"
                ftp_client.put(localfilepath1,remotefilepath1)
                ftp_client.close()
                stdin, stdout, stderr = ssh_client.exec_command('sudo su - root')
                stdin.write("\n")
                stdin.flush()
                stdin, stdout, stderr = ssh_client.exec_command("whoami")
                print(stdout.readlines())
          elif outData == -1:
                print("Host is unreachable - Please contact your network administrtor, nothing to do")
          else:
                print("mttrapd probe is not running on this host, nothing to do")

    file.close()

我收到的输出如下

Welcome on-board ...
Please choose from following
1 - Global Update
2 - Specific Update
 Please input as 1 or 2 :2
Please provide 'IPADDRESS' of Specific Remote Client :x.x.x.x
Trying to connect - x.x.x.x
[Probe = nco_p_mttrapd] and PID =  12345
['$user\n']
Trying to connect - x.x.x.x
[Probe = nco_p_mttrapd] and PID =  12345
**['$user\n'] #--------> I need this to be ['root\n']**

【问题讨论】:

  • 你运行一个命令,它完成,你的程序继续。大概看stackoverflow.com/questions/37586811/…
  • 无法 sudo su - root 是什么意思?有什么错误吗?
  • 从脚本执行 sudo su - root 时,我无法以 root 身份执行命令(在我的情况下为远程)。考虑您有一个脚本,您首先执行 sudo su - root 然后运行 ​​whoami 从同一个脚本并期望输出到 whoami 将是 root。但是,在我的情况下,whoami 给了我 $user 而不是 root,这是问题所在。希望这能解释...
  • @tripleee - 期待解决问题,我现在明白了。

标签: python linux python-3.x shell paramiko


【解决方案1】:

这行不通:

stdin, stdout, stderr = ssh_client.exec_command('sudo su - root')
stdin, stdout, stderr = ssh_client.exec_command("whoami")

与以下命令基本相同:

[local-host] # ssh user@host sudo su - root
[local-host] # ssh user@host whoami

你应该这样写:

stdin, stdout, stderr = ssh_client.exec_command('sudo su - root')
stdin.write('whoami\n')
# Here stdout.readlines() would not work as it would keep reading
# until EOF (which means the `sudo` comamnd has exited).
print(stdout.channel.recv(1024) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多