Jump to content

Recommended Posts

Well i run commands like this:

 

$ssh->exec('uptime');

 

and that all works fine its just a example, but how would i edit a config file via the ssh like that? but the thing is the config is in the following format:

 

hostname Example
port 7777
maxplayers 25

 

What sort of command would i need to run to edit just the line's port and maxplayers? because i need to change it to the lines maxplayers and port so people can't set random values how could i do this?

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/
Share on other sites

Sed is probably a great way to go.  One thing you could do is to grep through the file for the attribute that you are looking for (for example, maxplayers) and then use cut or awk to get the value.  If you're going to use cut then you might want to use "tr -s" first...just to compress all of the space characters into a single space.  Taking your example you could use either one of the examples below:

 

$ grep "maxplayers" config.txt | awk '{print $2}'
25
$ grep "maxplayers" config.txt | tr -s ' ' | cut -d ' ' -f 2 
25

 

Now that you have that value, you can use it along with the name of the attribute to do a substitution (maybe even do it globally?  Though I doubt that is necessary).  You probably don't want to substitute just the value by itself, otherwise you might wind up changing other attributes that just happen to have the same value (which isn't what you want to do).  Here is a quick shell script that works:

 

#!/bin/bash

confFile="config.txt"
confBack="config.back"
attribute=$1
newValue=$2

mv $confFile $confBack
oldValue=$(grep "$attribute" $confBack | awk '{print $2}')
oldString="$attribute $oldValue"
newString="$attribute $newValue"
sed -e "s/${oldString}/${newString}/" $confBack > $confFile

# optional -- delete the backup file
rm $confBack

 

You can try it out for yourself:

 

$ cat config.txt 
hostname Example
port 7777
maxplayers 25
$ ./update.sh maxplayers 30
$ cat config.txt 
hostname Example
port 7777
maxplayers 30

 

 

Perhaps not the most elegant solution but it definitely works!

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1051755
Share on other sites

Sorry guys, But i don't fully understand, I can't totally re-write the file because customers will have custom value's within the file like 'Hostname MyServer' etc i only need to change the value of the maxplayers and port value to the one one they have on file, I'm not sure i understand your methods ;/

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1051927
Share on other sites

Then maybe you should actually read the link I posted.

Wow...indeed.  I just read through it more thoroughly and realized there is a much simpler way to accomplish this than my script...as long as the ordering of the lines doesn't matter.  Just use sed to delete the appropriate line from the file with a regular expression and then append the new line with a >> redirect.  This makes for a much simpler 4 line solution.  :)

 

mv config.txt config.back
sed -e '/^maxplayers/d' config.back > config.txt
rm config.back
echo "maxplayers $newPlayerCount" >> config.txt

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1052801
Share on other sites

Then maybe you should actually read the link I posted.

Wow...indeed.  I just read through it more thoroughly and realized there is a much simpler way to accomplish this than my script...as long as the ordering of the lines doesn't matter.  Just use sed to delete the appropriate line from the file with a regular expression and then append the new line with a >> redirect.  This makes for a much simpler 4 line solution.  :)

 

mv config.txt config.back
sed -e '/^maxplayers/d' config.back > config.txt
rm config.back
echo "maxplayers $newPlayerCount" >> config.txt

 

I did this and it didn't work:

 

			$ssh->exec('
			cd /home'.$Fetch[Directory].' &&
			mv '.$PackageRow[ConfigFile].' config.back &&
			sed -e \'/^maxplayers/d\' config.back > '.$PackageRow[ConfigFile].' &&
			rm config.back &&
			echo \"maxplayers $Fetch[slots]\" >> '.$PackageRow[ConfigFile]);

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053675
Share on other sites

I don't normally write other people's code but here you go

 

$ssh->exec("sed -i -e '/^maxplayers/d' /home/{$Fetch['Directory']}/{$PackageRow['ConfigFile']} && echo 'maxplayers {$Fetch['Slots']}' >> /home/{$Fetch['Directory']}/{$PackageRow['ConfigFile']}");

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053901
Share on other sites

I don't normally write other people's code but here you go

 

$ssh->exec("sed -i -e '/^maxplayers/d' /home/{$Fetch['Directory']}/{$PackageRow['ConfigFile']} && echo 'maxplayers {$Fetch['Slots']}' >> /home/{$Fetch['Directory']}/{$PackageRow['ConfigFile']}");

 

Still did nothing :/

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1054102
Share on other sites

heh if the directories and the config file is correct it should work. I tested it on my system and it worked perfectly

 

I've managed to get part of it working the files were messed up but it just deletes the line 'maxplayers' from the file with its value, it doesn't replace it?.

Link to comment
https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1054197
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.