Let me rephrase the total question. I got already the config file for all vpcs using below script
#!/bin/bash
VPC="$(aws ec2 describe-vpcs | jq -r '.Vpcs[] | .VpcId')"
for x in $VPC
do
echo $x
aws ec2 describe-instances --filters "Name=vpc-id,Values=$x" | jq -r '.Reservations[].Instances[] | (.Tags[]//[]|select(.Key=="Name")|.Value) as $name | "Host \($name) \nHostname \(.PrivateIpAddress)"'
done
and this gave me output vpc wise with Host and Hostname like this
vpc-agdh5j6j
Host remote-server1
Hostname 123.45.6.6
Host remote-server2
Hostname 456.4.56.7
vpc-guh5jk6y
Host remote-server3
Hostname 245.24.789.9
Host remote-server4
Hostname 457.87.1.7
.
.
.
so on Now i saved this output to a file and trying to add one more line after Hostname wherever it exists in the file.
#!/bin/bash
echo "user : "$1
VPC="$(aws ec2 describe-vpcs | jq -r '.Vpcs[] | .VpcId')"
for x in $VPC
do
echo $x
aws ec2 describe-instances --filters "Name=vpc-id,Values=$x" | jq -r '.Reservations[].Instances[] | (.Tags[]//[]|select(.Key=="Name")|.Value) as $name | "Host \($name) \nHostname \(.PrivateIpAddress)"' > config.txt
sed -i '/^Hostname.*/a User\t$1' config.txt
cat config.txt
done
Here user is passed as an argument and is passing to script as mentioned above. It is not taking the input value given to "user" and is simply displaying in the output as $1 only. Its giving the output as below.
vpc-agdh5j6j
Host remote-server1
Hostname 123.45.6.6
User $1
Host remote-server2
Hostname 456.4.56.7
User $1
vpc-guh5jk6y
Host remote-server3
Hostname 245.24.789.9
User $1
Host remote-server4
Hostname 457.87.1.7
User $1
.
.
so on
Its not taking value given in the argument.
CodePudding user response:
Probably not a 100% fit as your requirements are a bit unclear, but this should get you started.
sed "s/^ *\(HostName .*\)/ \1\n User ${username}\n ProxyJump ${jump}\n/" ~/.ssh/config
Check that the output is as you would like it, then add -i
to sed
to make the changes in-place.
The logic:
s/
-- search/replace^ *\(HostName .*\)
-- the line on which to act^ *
-- start of line (^
followed by 0..n spaces)\(
-- starts a groupHostName
-- verbatim string.*
-- space, followed by 0..n characters (to end of line)\)
-- ends group
/
-- end of search pattern, start replace pattern\1
-- four spaces, followed by the first (and only) group from the search pattern\n
-- newlineUser ${username}\n
-- your User entryProxyJump ${jump}\n
-- your ProxyJump entry
/
-- end of replace pattern
CodePudding user response:
Script is working if i use double quotes for sed command it worked. Its taking variable value passed in the argument
sed -i "/^Hostname.*/a User\t$1" config.txt