0

I am building a script that generates an ssh config file out of a Supperputty XML Session config file. I don't know which tool could handle it better. I try to achieve it with awk, but I don't receive the expected results since the fields are never the same.

inputfile:

<SessionData SessionId="DMZ contoso.prod.net" SessionName="amq01.contoso.prod.net" ImageKey="computer" Host="amq01.contoso.prod.net" Port="22" Proto="SSH" PuttySession="[webserver]-[EL]# PROD | xx" Username="user" ExtraArgs="" SPSLFileName="" />
<SessionData SessionId="Build und Deploy/[Websphere]-[IR]# Deploy" SessionName="[Websphere]-[Internal]# Deploy" ImageKey="computer" Host="deploy.contoso.prod.net" Port="22" Proto="SSH" PuttySession="[Websphere]-[internal]# Deploy" Username="usa" ExtraArgs="" SPSLFileName="" />
<SessionData SessionId="ImportedFromPuTTY/[DP]-[DMZ]# PROD | 01" SessionName="[DP]-[DMZ]# PROD | 01" Host="192.168.1.91" Port="2354" Proto="SSH" PuttySession="[DP]-[DMZ]# PROD | 01" Username="schnipi" SPSLFileName="" />

expected Output:

#short info
Host Hostfromfile
  HostName amq01.contoso.prod.net
  User user
  Port 22

I already tried it some ways but only the third one somehow works, because SessionId always stays at the same position:

        awk 'if ( $2 ~ /SessionId="/,/\/"/ ) {print "#Test "$2}
        awk -F '="' '{print($2 == /SessionId/) "#Test " $2 ""}' session >> Test.conf
        awk -F '="' '{print "#Description: " $2 "\n" }' session >> Test.conf
        awk -F '="' '/SessionId=/ {for (a=1;a<=NF;++a) if ($a~/SessionId=/) print "# " $a "\n"} ' session >> config

I don't know how to only Output the match between the quotes without the quotes.

1

Try this,

IFS=$'\n'
for i in `cat file.xml`
do
        echo "$i" | awk -F '"' '{print "#Discription: " $2 "\nHost internal\n  HostName " $8"\n  User " $16 "\n  Port "$10 }'
done

output which i got as:

#Discription: DMZ contoso.prod.net
Host internal
  HostName amq01.contoso.prod.net
  User user
  Port 22
#Discription: Build und Deploy/[Websphere]-[IR]# Deploy
Host internal
  HostName deploy.contoso.prod.net
  User web
  Port 22
  • nice try and a good solution. Unfortunately my XML has 200Hosts and some have been imported from putty, what causes the result to shift the $fields. Example: Hostname: 22 User: Port SSH Like I said, The fields are not always the same. Thats why I try to catch where the Patterns like /patternA=/,/"/ /PatternB=/,/"/ I added an example Putty import – Mickey Mazurek Mar 20 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.