ChrisMartino Posted May 1, 2010 Share Posted May 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/ Share on other sites More sharing options...
trq Posted May 1, 2010 Share Posted May 1, 2010 Without actually logging in and editing the file within a text editor you would need to use sed which is whats called a stream editor. This is a nice beginners guide to its usage, though it can get a hell of allot more powerful if need be. Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1051678 Share on other sites More sharing options...
gamblor01 Posted May 2, 2010 Share Posted May 2, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1051755 Share on other sites More sharing options...
ChrisMartino Posted May 2, 2010 Author Share Posted May 2, 2010 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 ;/ Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1051927 Share on other sites More sharing options...
trq Posted May 2, 2010 Share Posted May 2, 2010 Then maybe you should actually read the link I posted. Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1052143 Share on other sites More sharing options...
gamblor01 Posted May 4, 2010 Share Posted May 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1052801 Share on other sites More sharing options...
tomfmason Posted May 4, 2010 Share Posted May 4, 2010 This makes for a much simpler 4 line solution. Here is a two liner sed -e '/^'${1}'/d' config.txt > config.back |mv config.back config.txt echo "$1 $2" >> config.txt Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053135 Share on other sites More sharing options...
ChrisMartino Posted May 5, 2010 Author Share Posted May 5, 2010 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]); Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053675 Share on other sites More sharing options...
Daniel0 Posted May 5, 2010 Share Posted May 5, 2010 This makes for a much simpler 4 line solution. Here is a two liner sed -e '/^'${1}'/d' config.txt > config.back |mv config.back config.txt echo "$1 $2" >> config.txt sed -i -e '/^'${1}'/d' config.txt && echo "$1 $2" >> config.txt Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053781 Share on other sites More sharing options...
ChrisMartino Posted May 5, 2010 Author Share Posted May 5, 2010 You guys got any idea why my posted code didn't work :[ Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053830 Share on other sites More sharing options...
Daniel0 Posted May 5, 2010 Share Posted May 5, 2010 Define "didn't work". Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053853 Share on other sites More sharing options...
tomfmason Posted May 6, 2010 Share Posted May 6, 2010 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']}"); Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1053901 Share on other sites More sharing options...
ChrisMartino Posted May 6, 2010 Author Share Posted May 6, 2010 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 :/ Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1054102 Share on other sites More sharing options...
tomfmason Posted May 6, 2010 Share Posted May 6, 2010 heh if the directories and the config file is correct it should work. I tested it on my system and it worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1054167 Share on other sites More sharing options...
ChrisMartino Posted May 6, 2010 Author Share Posted May 6, 2010 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?. Quote Link to comment https://forums.phpfreaks.com/topic/200390-edit-file-via-ssh/#findComment-1054197 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.