从user1@host1
我希望在远程主机上执行docker命令,即root@host2
因此,在远程host2
上,我做了以下更改:
sudo visudo
user1 ALL=(ALL) NOPASSWD: /bin/docker
dbuser ALL=(ALL) NOPASSWD: /bin/docker
字符串
我使用下面的ssh命令从host1
登录到host2
:
[root@host1]# ssh -i /home/user1/.ssh/id_rsa user1@host2
[user1@host2 ~]# sudo -u root -i -H
[root@host2 ~]# sudo docker exec STG-MYDB-mongo-rs mongo --port 27062 --authenticationDatabase '$external' --authenticationMechanism PLAIN -u 'dbuser' -p 'dbpassword' --eval 'rs.isMaster()'
MongoDB shell version v4.2.20
connecting to: mongodb://127.0.0.1:27062/?authMechanism=PLAIN&authSource=%24external&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cadeffab-911e-4290-9455-4aabcb232dfd") }
MongoDB server version: 4.2.20
{
"hosts" : [
"host2.ec2.internal:27062",
"host3.ec2.internal:27062",
"host4.ec2.internal:27062"
],
"setName" : "REPGBMA062",
"setVersion" : 10,
"ismaster" : true,
"secondary" : false,
"primary" : "host2.ec2.internal:27062",
"me" : "host2.ec2.internal:27062",
"electionId" : ObjectId("7fffffff0000000000000129"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1689546619, 1),
"t" : NumberLong(297)
},
"lastWriteDate" : ISODate("2023-07-16T22:30:19Z"),
"majorityOpTime" : {
"ts" : Timestamp(1689546619, 1),
"t" : NumberLong(297)
},
"majorityWriteDate" : ISODate("2023-07-16T22:30:19Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2023-07-16T22:30:23.089Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 7620,
"minWireVersion" : 0,
"maxWireVersion" : 8,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1689546619, 1),
"signature" : {
"hash" : BinData(0,"bPyk8MEnMN/ThBc8m1tdsVjOcN8="),
"keyId" : NumberLong("7216872790308536321")
}
},
"operationTime" : Timestamp(1689546619, 1)
}
[root@host2 ~]# sudo docker exec STG-MYDB-mongo-rs mongo --port 27062 --authenticationDatabase '$external' --authenticationMechanism PLAIN -u 'dbuser' -p 'dbpassword' --eval 'rs.isMaster()' | grep primary
"primary" : "host2.ec2.internal:27062",
型
然而,当我通过ssh尝试相同的命令时,它失败并返回一个错误,并且没有返回与我手动运行命令时收到的相同的输出。
故障输出:
[root@host1 actions-runner]# ssh -t -i /home/user1/.ssh/id_rsa user1@host2.ec2.internal "sudo docker exec STG-MYDB-mongo-rs mongo --port 27062 --authenticationDatabase '$external' --authenticationMechanism PLAIN -u 'dbuser' -p 'dbpassword' --eval 'rs.isMaster()'"
##################################################################
# *** This Server is using Centrify *** #
# *** Remember to use your Active Directory account *** #
# *** password when logging in *** #
##################################################################
MongoDB shell version v4.2.20
connecting to: mongodb://127.0.0.1:27062/?authMechanism=PLAIN&compressors=disabled&gssapiServiceName=mongodb
2023-07-16T22:32:32.073+0000 E QUERY [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:353:17
@(connect):3:6
2023-07-16T22:32:32.074+0000 F - [main] exception: connect failed
2023-07-16T22:32:32.074+0000 E - [main] exiting with code 1
Connection to host2.ec2.internal closed.
[root@host1 actions-runner]#
型
如何使用远程ssh命令获得与手动运行命令相同的输出?
1条答案
按热度按时间cgfeq70w1#
当您发出SSH远程命令时:
字符串
...
$external
部分由shell本地插值,因为它在双引号字符串中。因此,如果变量$external
没有在本地定义,实际上发送的是一个空值(例如:--authenticationDatabase ''
)。简单地转义美元字符(\$
)应该足以使命令按预期工作。干杯