Global variable not defined after seperating functions to seperate Python file
<p>Continuing to write my dictionary attack script for project. </p>

<p>My script calls upon two functions that perform an actual dictionary attack on SSH. First I declared my global variable in my get_args() function in <code>main.py</code>, following with the main() function</p>

<pre><code>def get_args():
    # stuff for parsing blah blah


    global service, username, wordlist, address, port, delay

    service = args.service
    username = args.username
    wordlist = args.password
    address = args.address
    port = args.port
    delay = args.delay

    return service, username, wordlist, address


def main():

    service, username, wordlist, address = get_args()

    # output and stuff

    # SSH bruteforce
    if service == 'ssh':
        if address is None:
            print R + "[!] You need to provide a SSH address for cracking! [!]" + W
    else:
        print C + "[*] Address: %s" % address + W
        sleep(0.5)
        global port
        if port is None:
            print O + "[?] Port not set. Automatically set to 22 for you [?]" + W
            port = 22

        print C + "[*] Port: %s "  % port + W
        sleep(1)
        print P + "[*] Starting dictionary attack! [*]" + W
        print "Using %s seconds of delay. Default is 1 second" % delay
        sshBruteforce(address, username, wordlist)
</code></pre>

<p>The line <code>sshBruteforce(address, username, wordlist)</code> references a function that I include in a seperate file as a module, which I import with </p>

<pre><code>from module1 import *
</code></pre>

<p>Inside module1.py:</p>

<pre><code># ssh_connect()
def ssh_connect(password, code=0):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    paramiko.util.log_to_file("filename.log")

    try:
        ssh.connect(address, port=port, username=username, password=password)


def sshBruteforce(address, username, wordlist):
    wordlist = open(wordlist, 'r')

    for i in wordlist.readlines():
        password = i.strip("\n")
        try:
            response = ssh_connect(password)
            # actual bruteforcing here. The above line references the ssh_connect()
            # function, which is where the problem actual occurs
</code></pre>

<p>The line, <code>ssh.connect(address, port=port, username=username, password=password)</code> is where the problem occurs. Let's say I execute the script like so:</p>

<pre><code>python main.py -u root -w wordlist.txt -s ssh -p 22 -a 192.168.1.3 
</code></pre>

<p>This stores the string <code>"root"</code> within the variable <code>username</code>, and etc. However, once the <code>main.py</code> program executes the sshBruteforce() function, this occurs:</p>

<pre><code>global name 'address' is not defined
</code></pre>

<p>I know that this occurs within the ssh_connect() function, with line <code>ssh.connect(address, port=port, username=username, password=password)</code>, meaning that the variable <code>address</code> does not have anything stored in it. I do not know why this occurring. </p>

<p>Including <code>from __main__ import *</code> within <code>module1.py</code> does not change anything. I have seen many these questions asked, but none are similar to my situation. Any help is appreciated thanks!</p>

<p>The concept of global defining means that you should define it outside of the function, try to init all globals outside fuction with None or something and see if the problem is resolved</p>

python module global-variables