Raised This Month: $32 Target: $400
 8% 

Shell script to check if server's empty


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dustinandband
Senior Member
Join Date: May 2015
Old 10-12-2018 , 16:55   Shell script to check if server's empty
Reply With Quote #1

1)
Is it possible for a bash shell script to query a game server and determine if it’s empty or has players? If so, how is this done? (Even if you'd need a c++ extension installed to do so, I'd be willing to do the research with an idea of how it would work.)

2)
Is it possible for a shell script to monitor a screen session for a key word? The sourcemod equivalent of adding a command listener.
"MasterRequestRestart" is the word I'm wanting to pick up.
dustinandband is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 10-13-2018 , 02:12   Re: Shell script to check if server's empty
Reply With Quote #2

  1. There's a couple of ways. You can either send the A2S_INFO (Source Server) query via some application, use a standalone rcon tool to read the status output, or write some plugin using the socket extension to respond with your desired info
  2. Don't think so. Not sure if the logging facilities would catch that.
Any particular reason why a SteamWorks-based plugin that gets forwarded an event on restart request won't work for you?
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
Maxximou5
AlliedModders Donor
Join Date: Feb 2013
Old 10-13-2018 , 02:45   Re: Shell script to check if server's empty
Reply With Quote #3

2) Log the output of the console and use a cronjob to grep for whatever phrase you wish for.
Example
Maxximou5 is offline
dustinandband
Senior Member
Join Date: May 2015
Old 10-13-2018 , 18:06   Re: Shell script to check if server's empty
Reply With Quote #4

Quote:
Originally Posted by nosoop View Post
  1. There's a couple of ways. You can either send the A2S_INFO (Source Server) query via some application, use a standalone rcon tool to read the status output, or write some plugin using the socket extension to respond with your desired info
  2. Don't think so. Not sure if the logging facilities would catch that.
Any particular reason why a SteamWorks-based plugin that gets forwarded an event on restart request won't work for you?
Thanks.

I've compiled the rcon tool, but I'm not sure if I'm using it correctly.
In this example, the rcon password is just a made up password.

Code:
root@testsvrchi:~# ./rcon --help
Usage: <programname> --rcon="<ip> <port> <rconpw> <cmd>"
Example: --rcon="127.0.0.1 27960 myrconpw devmap oasis"

root@testsvrchi:~# ./rcon --rcon="74.91.124.177 27015 rconpassword101 status"
Sending:
¦¦¦¦rcon rconpassword101 status

Could not receive.
Not sure if there's a more specific rcon command to query "players - humans" count, or if it's something that would have to be parsed from the status command.
Code:
] rcon status
hostname: Left 4 Dead 2
version : 2.1.5.2 7149 secure  (unknown)
udp/ip  : 74.91.124.177:27015 [ public same ]
os      : Linux Dedicated
map     : c2m1_highway
players : 1 humans, 0 bots (4 max) (not hibernating) (reserved 186000000a383ab)
Edit:
Haven't worked with c++ / c before, but it looks like the error is stemming from this returning -1:
Code:
    n = recvfrom(sockfd, srline, MAX_SR_LINE, 0, NULL, NULL);
    if(n < 0) {
        close(sockfd);
        return RC_RECV;
    }
http://pubs.opengroup.org/onlinepubs.../recvfrom.html

Edit#2
Forgot to mention I tried the local IP as well
Code:
./rcon --rcon="127.0.0.1 27015 rconpassword101 status"

Last edited by dustinandband; 10-13-2018 at 19:03.
dustinandband is offline
hustl4
Junior Member
Join Date: Jan 2016
Location: USA
Old 10-14-2018 , 18:09   Re: Shell script to check if server's empty
Reply With Quote #5

why not use python?

PHP Code:
pip install python-valve 
PHP Code:
import valve.rcon
import re

ip 
"10.0.0.1"
port 27015
password 
"rconpassword"


def isEmpty(addresspassword):
    
with valve.rcon.RCON(addresspassword) as rcon:
        
response rcon.execute("status")
        
response_text response.body.decode('ascii''ignore')
        for 
line in response_text.splitlines():
            if 
"players :" in line:
                for 
words in ['0 humans']:
                    if 
re.search(r'\b' words r'\b'line):
                        return 
True
                    
else:
                        return 
False
            
else:
                
pass


if __name__ == "__main__":
    if 
isEmpty((ipport), password):
        print(
"server is empty")
    else:
        print(
"server is NOT empty"
hustl4 is offline
dustinandband
Senior Member
Join Date: May 2015
Old 10-14-2018 , 19:28   Re: Shell script to check if server's empty
Reply With Quote #6

@ hustl4
Thanks for the idea. The script appears to be getting hung up. If you do happen to test it out - would you mind uploading it to github so I can include a direct download for the script I'm currently writing? Would be best if there was safeguards against situations where it might get hung up - maybe there could be a timeout feature if the server doesn't respond in x amount of time there's a certain return value. (Since this script is designed to get launched automatically from a cron job task.)

In regards to the auto-updating:
- I can use screen to log output to a file and successfully added update functionality there, but the problem is when you have multiple servers on the same machine and need to make a unique log file name that's the same as the screen session (socket) name.
- Some users say to use tmux instead of screen, so I spent the better part of a day writing out a script to handle auto-launching X amount of servers:
(I still have to make the tmux log.)
Spoiler

- Come to find out after all that work, the latest version of tmux may not be compatible with creating dynamic log names (if i revert to an older version of tmux and ever 'apt-get update', it may break the script):
https://github.com/GameServerManager..._start.sh#L113

Anyway, if that's the case I'll just use something similar to what hustl4 posted. If all game servers are all detected as empty on some night, they'll all get an update then the physical server will do a reboot. The cron job task will relaunch the script ^ and the servers will be back online.

Last edited by dustinandband; 10-16-2018 at 11:41.
dustinandband is offline
hustl4
Junior Member
Join Date: Jan 2016
Location: USA
Old 10-15-2018 , 04:46   Re: Shell script to check if server's empty
Reply With Quote #7

Quote:
Originally Posted by dustinandband View Post
@ hustl4
Thanks for the idea. The script appears to be getting hung up. If you do happen to test it out - would you mind uploading it to github so I can include a direct download for the script I'm currently writing? Would be best if there was safeguards against situations where it might get hung up - maybe there could be a timeout feature if the server doesn't respond in x amount of time there's a certain return value. (Since this script is designed to get launched automatically from a cron job task.)
sorry, should have mentioned only tested on python3.6 on a mix of centos 7 and ubuntu 16.04 (csgo servers)
I have it running in cron every minute (cpu mining crypto when servers are empty lmao)
I added a timeout exception and uploaded to github
https://gist.github.com/wh1te909/82f...7fed2b8fd7dcf8
hustl4 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:41.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode