简体   繁体   中英

How could I modify the ssh_config file on a linux host with perl?

I have a interesting problem, my script uses ssh to access my vmware server in order to pull data from it. I have discovered that if the machine running the script has never connected to the server the script will fail to run because the question asking if the host I am connected to should be added to my known hosts file.

Based on reading some forums and such and people having a similar issue I came up with an idea. I wanted to modify the ssh_config file on the "script host" to exclude known host checking for the host that specified in the config file. This would be done by the config script I have made that ask you various questions that generate the xml config file.

Now for the problem...

I need modify the config file, I was thinking the easiest would be slurp the file into perl and manipulate it. Slurping I have done before but according to what I have read the directive for disabling certain ssh options on a perticular host has to go before the global options directive.

How can I insert text in between 2 blocks of text in the current file?

This is what my ssh_config looks like

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

**--This is what my modification need to go--**

Host *
#   ForwardAgent no
#   ForwardX11 no
#   ForwardX11Trusted yes
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   Port 22
#   Protocol 2,1
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160

You can access the lines of a file via a Perl array with the core Tie::File module. For example, to insert a line using offset:

use Tie::File;
tie @array, 'Tie::File', $file or die "Can't tie $file";
splice @array, $offset, 0, $line;

You can also loop through the array, etc. Changes to the array are reflected in the file immediately.

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